Interview Prep9 min read

Top 25 SQL Interview Questions with Answers (2026)

SQL shows up in data analyst, data engineer and backend interviews alike, and the questions are remarkably consistent. If you can write joins fluently, reason about grouping, and predict what a query returns, you'll clear most SQL rounds. Here are the recurring questions, answered.

A laptop showing a code editor with a database icon and query brackets, representing SQL and databases

Joins - the heart of every SQL interview

Inner vs left vs right vs full outer join?

Inner join keeps only rows that match in both tables. Left join keeps all left-table rows (NULLs where the right has no match); right join is the mirror image. Full outer join keeps all rows from both, matched where possible.

What is a self join and when is it used?

A self join joins a table to itself using aliases. Classic example: an employees table joined to itself on manager_id to list each employee alongside their manager.

Join vs subquery - which do you use?

Joins combine columns from multiple tables into one result; subqueries nest a query to produce a value or filter. Joins often read more clearly and let the optimiser work better when you're combining data, while subqueries suit existence checks and derived values.

Grouping, filtering & functions

GROUP BY, and the difference between WHERE and HAVING?

GROUP BY collapses rows into groups for aggregation. WHERE filters individual rows before grouping; HAVING filters the aggregated groups after. You can't use an aggregate in WHERE - that's what HAVING is for.

How do NULLs affect aggregate functions?

COUNT(*) counts every row, but COUNT(column), SUM and AVG ignore NULLs. This is a classic trap - an average over a column with NULLs divides by the count of non-null values, not all rows.

What are window functions?

Functions like ROW_NUMBER, RANK and running totals compute a value across a set of rows (a 'window') without collapsing them like GROUP BY does. They're ideal for 'top N per group' and running-total questions.

Performance & correctness

What is an index and what does it cost?

An index is a sorted structure (often a B-tree) that lets the engine find rows without scanning the whole table, speeding up reads. The cost is slower writes and extra storage, since the index must be updated too.

Primary key vs unique key vs foreign key?

A primary key uniquely identifies a row and can't be null (one per table). A unique key enforces uniqueness but allows a null. A foreign key references another table's key to enforce referential integrity.

DELETE vs TRUNCATE vs DROP?

DELETE removes selected rows and can be rolled back (it's logged per row). TRUNCATE quickly empties a whole table. DROP removes the table structure entirely.

How do you answer a 'predict the output' question?

Read the query against the sample rows step by step - apply WHERE, then GROUP BY, then HAVING, then SELECT - and state exactly which rows come back. This is the single most common live SQL test, so practise it.

The bottom line

Now go test yourself

SQL is one of the highest-return skills you can drill for an interview: the question set is small, stable, and shared across data, analyst and backend roles. Master joins, grouping, subqueries and indexes, and learn to read a query and state its output, and you'll clear most SQL rounds comfortably.

The best way to build query-reading speed is repetition. Start an SQL quiz, predict each output before you answer, and let the explanations sharpen the patterns until they're instinct.

FAQs

Frequently asked questions

Which SQL topics are most important for interviews?

Joins, GROUP BY with HAVING, subqueries, indexes, and query-output prediction. Add window functions to stand out.

Which SQL dialect should I learn for interviews?

Stick to standard ANSI SQL - joins, grouping and subqueries work the same across MySQL, PostgreSQL and SQL Server, which is what most interviews test.

How do I get faster at SQL queries?

Practice reading queries and predicting their output. A timed MCQ round with query-output questions builds the pattern recognition interviews reward.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping; HAVING filters aggregated groups after GROUP BY. You can't use an aggregate like COUNT() in WHERE - that's exactly what HAVING is for.

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table, filling NULLs where the right table has no match - useful for finding records with no related rows.

What are window functions and why are they asked?

Window functions (ROW_NUMBER, RANK, running totals) compute values across a set of rows without collapsing them like GROUP BY. They're increasingly asked because 'top N per group' and running-total questions are common in real analytics work.

How do NULLs affect SQL aggregate functions?

COUNT(*) counts all rows, but COUNT(column), SUM and AVG ignore NULLs. This trips people up - an average over a column with NULLs divides by the count of non-null values, not the total row count.

What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes selected rows and can be rolled back; TRUNCATE quickly empties an entire table; DROP removes the table structure itself. They differ in scope, speed and reversibility.

Do I need to know database design for a SQL interview?

For pure SQL rounds, querying matters most, but many interviews blend in DBMS concepts like normalization, keys and indexing - so brushing up on database fundamentals is worthwhile.

Related quizzes

Put it into practice

Keep reading

Related articles

Browse all articles →

Test yourself in two minutes

Six adaptive questions, every answer explained by an AI tutor. Free.

▶ Start an AI quiz