SQL: Interview questions with tested queries
SQL is tested in almost every data, analyst, backend and QA interview in India. Expect a mix of concept questions (joins, keys, NULL), query writing on a whiteboard or online editor, and a short performance or design discussion. This kit works for any employer.
27 questions · 3 rounds · updated Nov 2026
How the interview works
- Concept round: Joins, keys, NULL handling, normalisation, transactions.
- Query writing: Ranking, duplicates, anti-joins, window functions, date filters.
- Performance and design: Indexes, execution plans, table design for a small product.
Sample questions
- 01
How does NULL behave in comparisons, aggregates and NOT IN?
MediumFundamentals
Model answer
What the interviewer checks
Three-valued logic and the real bugs it causes.
How to answer
NULL means unknown, so comparisons with it give UNKNOWN, which WHERE treats as false. • `WHERE manager_id = NULL` returns nothing. Use `IS NULL`. • Aggregates skip NULLs: AVG(bonus) averages only non-NULL bonuses, and COUNT(bonus) counts non-NULL values while COUNT(*) counts rows. If NULL should mean 0, use AVG(COALESCE(bonus, 0)). • `NOT IN (subquery)` returns no rows if the subquery contains a single NULL, because `x <> NULL` is unknown. Use NOT EXISTS instead. • In GROUP BY, all NULLs form one group. In ORDER BY, PostgreSQL puts NULLs last for ascending order and MySQL puts them first.
- 02
customers has 4 rows. orders has 6 rows, all belonging to 2 of those customers. How many rows does `customers LEFT JOIN orders ON orders.customer_id = customers.id` return?
MediumJoins
- 03
Find the second highest salary. Then generalise to the Nth highest. If there is no such salary, return NULL.
MediumQuery writing
Approach and solution
Approach
DENSE_RANK handles ties: two people on the top salary both get rank 1, and the next salary is rank 2. Wrapping in a subquery with MAX returns NULL when no row matches. LIMIT/OFFSET on DISTINCT salaries also works, but returns no row instead of NULL.
-- Second highest (works everywhere) SELECT MAX(salary) AS second_highest FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); -- Nth highest, here N = 3 SELECT MAX(salary) AS nth_highest FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees ) t WHERE rnk = 3;Complexity: One sort of the salary column; an index on salary makes it a quick index scan.
Likely follow-ups
- RANK vs DENSE_RANK vs ROW_NUMBER for salaries 100, 100, 90: RANK gives 1, 1, 3; DENSE_RANK gives 1, 1, 2; ROW_NUMBER gives 1, 2, 3.
- Nth highest per department: add PARTITION BY dept_id inside OVER().
24 more questions, members only
- Fundamentals10
- Query writing12
- Scenarios and performance5
Members also get how to answer each question and a preparation plan for this company.
See plans from ₹399Every question and answer is written by Hirecept and the code is run before it is published. How a company runs its process comes from candidate reports and public pages, and companies change it, so treat that part as a guide, not a promise.