SQL Interview Questions
SQL is tested in data, engineering, analytics, and backend roles. These questions cover everything from basic queries to advanced window functions, performance optimization, and complex multi-table analysis.
Sample Questions & Strong Answers
What is the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN?
INNER JOIN returns only rows where the join condition matches in both tables. LEFT JOIN returns all rows from the left table, plus matching rows from the right (NULL for non-matches). RIGHT JOIN is the mirror. FULL OUTER JOIN returns all rows from both tables, with NULLs where there's no match.
Pro tip: Draw a Venn diagram mentally when choosing join types. In practice, INNER and LEFT JOINs cover 95%+ of use cases.
What is a window function? Give an example.
Window functions perform calculations across a set of rows related to the current row, without collapsing rows (unlike GROUP BY). Example: SELECT name, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) as salary_rank FROM employees;
Pro tip: Know ROW_NUMBER (unique rank), RANK (gaps allowed), DENSE_RANK (no gaps), LAG/LEAD (previous/next rows), and SUM/AVG OVER PARTITION BY.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping/aggregation. HAVING filters groups after aggregation. Use WHERE to filter individual rows on non-aggregated columns; use HAVING to filter results of aggregate functions like COUNT, SUM, AVG.
Pro tip: You can use both in the same query: WHERE filters raw rows, HAVING filters the aggregated groups.
How do you find duplicate rows in a table?
SELECT column, COUNT(*) as count FROM table GROUP BY column HAVING COUNT(*) > 1. For multiple columns, group by all the columns that define uniqueness.
Pro tip: A follow-up is often 'how do you delete duplicates while keeping one row?' — know ROW_NUMBER with DELETE for this.
Expert tips
Master the order of SQL operations: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
Window functions are the most commonly tested advanced SQL topic — practice ROW_NUMBER, RANK, LAG, LEAD, and SUM OVER PARTITION BY.
For complex queries, build incrementally: start with a subquery or CTE, verify it works, then add the outer query.
Always think about edge cases: NULL values, empty tables, ties in ranking, and duplicate keys.
Related interview topics
SQL Interview Questions FAQ
Practice SQL Interview Questions with real-time AI.
Stop reading sample answers and start practicing. Get personalized feedback on structure, delivery, and depth — every time.
Free plan available · No credit card needed · Cancel anytime