Tech

SQL

SQL (Structured Query Language) is the universal language for querying and managing relational databases. Whether you are a backend engineer, data analyst, or product manager, the ability to write efficient queries is a foundational skill. Interview questions focus on joins, aggregations, indexing strategies, normalization, and query optimization — skills that translate directly to real-world database work.

What you get

Questions

20

Difficulty

3 levels

Answer Formats

2

Use the toggle on each card to move between an interview-ready answer and a simpler explanation. Questions are sorted from beginner to advanced, and the keywords are highlighted. You can also blur the answers to practice recalling them from memory.

Questions

Practice the answers out loud.

All topics

Question 1

What is SQL?

Beginner

How to answer in an interview

SQL, or Structured Query Language, is a standard language used to communicate with and manipulate relational databases. It's used to query, insert, update, and delete data, as well as define and manage database structures like tables, views, and indexes.

Question 2

What is the difference between SQL and NoSQL databases?

Beginner

How to answer in an interview

SQL databases are relational, use structured schemas with tables and rows, and enforce ACID compliance, making them ideal for structured data with complex relationships. NoSQL databases are typically schema-less, store data as documents, key-value pairs, or graphs, and are designed for horizontal scalability and flexible or unstructured data.

Question 3

What is the difference between a primary key and a foreign key?

Beginner

How to answer in an interview

A primary key uniquely identifies each record in a table and cannot contain null values. A foreign key is a column in one table that references the primary key of another table, establishing a relationship between the two tables and enforcing referential integrity.

Question 4

What is the difference between WHERE and HAVING clauses?

Beginner

How to answer in an interview

WHERE filters individual rows before any grouping occurs and cannot be used with aggregate functions. HAVING filters groups after a GROUP BY has been applied and is used specifically to filter based on aggregate function results, like COUNT or SUM.

Question 5

What is the difference between DELETE, TRUNCATE, and DROP?

Beginner

How to answer in an interview

DELETE removes specific rows based on a condition and can be rolled back, and it's a DML command that fires triggers. TRUNCATE removes all rows from a table quickly without logging individual row deletions, and it resets identity counters. DROP removes the entire table structure along with its data permanently.

Question 6

What are aggregate functions in SQL?

Beginner

How to answer in an interview

Aggregate functions perform a calculation on a set of rows and return a single summary value. Common examples include COUNT(), SUM(), AVG(), MIN(), and MAX(), and they're often used together with GROUP BY to summarize data by category.

Question 7

Explain GROUP BY and its use with aggregate functions.

Beginner

How to answer in an interview

GROUP BY organizes rows that share the same value in specified columns into summary groups, typically used alongside aggregate functions like SUM or COUNT to calculate per-group results, such as total sales per region.

Question 8

What is the difference between UNION and UNION ALL?

Beginner

How to answer in an interview

UNION combines the result sets of two or more SELECT queries and removes duplicate rows, which requires extra processing. UNION ALL also combines results but keeps all duplicates, making it faster since no duplicate-checking is needed.

Question 9

Explain the different types of JOINs in SQL.

Intermediate

How to answer in an interview

An INNER JOIN returns only matching rows from both tables. A LEFT JOIN returns all rows from the left table plus matched rows from the right, filling unmatched right-side columns with NULL. A RIGHT JOIN does the opposite, and a FULL OUTER JOIN returns all rows from both tables, matching where possible and filling in NULLs elsewhere.

Question 10

What is normalization in databases?

Intermediate

How to answer in an interview

Normalization is the process of organizing database tables to reduce data redundancy and improve data integrity, typically by dividing large tables into smaller related tables and defining relationships between them. It's implemented through a series of normal forms (1NF, 2NF, 3NF, etc.), each addressing specific types of redundancy.

Question 11

What is an index and how does it improve performance?

Intermediate

How to answer in an interview

An index is a database structure, typically a B-tree, that allows faster lookups of rows based on the values of one or more columns, similar to a book's index. Without an index, the database must scan the entire table, but with one, it can quickly locate matching rows, greatly speeding up SELECT queries at the cost of slower writes and extra storage.

Question 12

What is a transaction in SQL?

Intermediate

How to answer in an interview

A transaction is a sequence of one or more SQL operations executed as a single logical unit of work, using COMMIT to save changes permanently or ROLLBACK to undo them if an error occurs. Transactions ensure that a set of related operations either fully succeed or fully fail, preserving data integrity.

Question 13

What is the difference between a subquery and a JOIN?

Intermediate

How to answer in an interview

A subquery is a query nested inside another query, often used to filter results based on a computed value, while a JOIN combines rows from two or more tables based on a related column directly. JOINs are generally more efficient for combining data from multiple tables, while subqueries are useful for more isolated, conditional logic.

Question 14

What is a stored procedure?

Intermediate

How to answer in an interview

A stored procedure is a precompiled set of SQL statements stored in the database that can be executed as a single callable unit, often accepting parameters. They improve performance through precompilation, promote code reuse, and encapsulate business logic close to the data.

Question 15

What is a view in SQL?

Intermediate

How to answer in an interview

A view is a virtual table based on the result of a stored SQL query, which doesn't store data itself but presents data from one or more underlying tables. Views simplify complex queries, provide a layer of abstraction, and can restrict access to specific columns or rows for security purposes.

Question 16

What is SQL injection and how do you prevent it?

Intermediate

How to answer in an interview

SQL injection is a security vulnerability where an attacker inserts malicious SQL code into an input field to manipulate or access the database in unintended ways. It's prevented by using parameterized queries or prepared statements instead of concatenating user input directly into SQL strings, along with input validation and least-privilege database accounts.

Question 17

What is denormalization and why would you use it?

Advanced

How to answer in an interview

Denormalization intentionally introduces redundancy into a database design, often by merging tables or duplicating data, to improve read performance by reducing the number of joins needed. It's a trade-off used in read-heavy systems like reporting or analytics, at the cost of higher storage use and more complex data consistency management.

Question 18

Explain the ACID properties of a database transaction.

Advanced

How to answer in an interview

ACID stands for Atomicity, Consistency, Isolation, and Durability. Atomicity ensures a transaction is all-or-nothing, Consistency ensures the database moves from one valid state to another, Isolation ensures concurrent transactions don't interfere with each other, and Durability ensures committed changes survive system failures.

Question 19

What is a deadlock and how can it be avoided?

Advanced

How to answer in an interview

A deadlock occurs when two or more transactions are waiting for each other to release locks, causing them to be stuck indefinitely. It can be avoided by acquiring locks in a consistent order across transactions, keeping transactions short, using appropriate isolation levels, and allowing the database's deadlock detection to roll back one of the conflicting transactions.

Question 20

What is the difference between a clustered and non-clustered index?

Advanced

How to answer in an interview

A clustered index determines the actual physical order of data in the table, so there can only be one per table, and it's typically built on the primary key. A non-clustered index maintains a separate structure with pointers back to the actual rows, allowing multiple non-clustered indexes per table for different query patterns.

More in Tech

Keep browsing related topics.

Browse all