SQL Interview Questions for Developers (2026): From Joins to Query Optimization

By Preeti Verma

SQL interviews for backend and full-stack roles rarely stop at "write a SELECT query". Interviewers want to know whether you understand what the database is doing under your query, and whether you can make a slow one fast. Here are the questions that come up most, in the order they usually build on each other.

Joins

How do the join types differ? INNER JOIN returns only matching rows. LEFT JOIN keeps every row from the left table, filling unmatched right-side columns with NULL. RIGHT JOIN is the mirror image and is rarely used in practice because you can just swap the table order and use LEFT JOIN. FULL OUTER JOIN keeps unmatched rows from both sides.

When would you use a self-join? When a table references itself, such as an employee table with a manager_id pointing to another row in the same table. You join the table to itself with an alias for each side.

What is the difference between WHERE and HAVING? WHERE filters rows before grouping; HAVING filters groups after aggregation. A common mistake is trying to filter on an aggregate (like COUNT(*) > 5) in WHERE, which fails because the aggregate does not exist yet at that stage.

Keys and constraints

Primary key versus unique key? A table has exactly one primary key, which cannot be NULL. It can have several unique keys, and a unique key column can allow one NULL (behavior varies slightly by database). Both enforce no duplicate values.

What is a foreign key, and why bother with it if the application already validates the relationship? It enforces referential integrity at the database level, so a bad insert or a bug in one service cannot silently create an orphaned row. It also documents the relationship for anyone reading the schema.

Indexing and performance

How does an index actually speed up a query? Most indexes are a B-tree, so a lookup that would scan every row becomes a lookup that walks a small number of tree levels. The trade-off is that every insert or update also has to update the index, so indexing every column blindly slows writes.

A query is slow, an index exists on the filtered column, and it is still slow. What would you check? Whether the index is actually used (via EXPLAIN), whether a function is applied to the column in the WHERE clause (which usually prevents index use), whether the column has low cardinality (an index on a boolean column rarely helps), and whether the query returns so many rows that a full scan is actually cheaper.

What is a composite index, and does column order matter? An index on more than one column. Order matters: an index on (user_id, created_at) speeds up a query filtering on user_id alone or on both columns together, but not one filtering on created_at alone.

Normalization and design

What are the normal forms, and would you always normalize fully? Briefly: 1NF removes repeating groups, 2NF removes partial dependencies on a composite key, 3NF removes transitive dependencies. In practice, many production schemas deliberately denormalize a little (duplicating a value to avoid a join) for read-heavy paths, and a good answer explains that trade-off rather than reciting the definitions.

Aggregation and window functions

Find the second-highest salary. What if several people share the top salary? A common approach: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). This correctly skips ties at the top. Interviewers often follow up asking for the Nth highest, which is a natural use for DENSE_RANK().

What is a window function, and how does it differ from GROUP BY? GROUP BY collapses rows into one row per group. A window function (like ROW_NUMBER(), RANK() or a running SUM() OVER (...)) computes a value per row while still seeing the group it belongs to, so you keep the original rows. This is the tool for "top N per group" and running totals.

Write a query that finds duplicate rows in a table. Group by the columns that define a duplicate and filter with HAVING COUNT(*) > 1. A follow-up usually asks how you would then delete all but one copy, which needs a window function or a self-join on the primary key.

Transactions

What do the ACID properties mean in practice? Atomicity means a transaction fully happens or fully does not. Consistency means it leaves the database in a valid state. Isolation means concurrent transactions do not see each other's uncommitted changes. Durability means a committed transaction survives a crash. A strong answer gives an example, such as a bank transfer, for at least one of these.

What is a deadlock, and how would you avoid one? Two transactions each hold a lock the other needs. The usual prevention is consistent lock ordering (always lock rows in the same order across the application) and keeping transactions short.

How to prepare

Practise writing queries by hand, not just reading them, since interviewers often ask you to write SQL live or on a whiteboard. Also practise explaining your reasoning out loud as you write, since that is what is actually being graded. Our Core Java interview questions guide covers the backend side these SQL questions usually pair with, and the AI Coach can walk through any of these topics in simple words if one feels shaky.