Data

SQL interview questions

Querying, joins, aggregation, window functions, and query-performance reasoning, the SQL interviewers actually probe, from analyst screens to backend loops.

5 questions (2 easy · 2 medium · 1 hard), each with what a strong answer covers and where people lose the point. Free to read, no account.

On this page (5 questions)

1.What is the difference between WHERE and HAVING?

Warm-up

What a strong answer covers

  • WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY has aggregated them.
  • Aggregate functions (COUNT, SUM, AVG) can appear in HAVING but not in WHERE, because aggregates don't exist yet at row-filtering time.
  • Logical evaluation order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
  • Performance note: filtering early in WHERE shrinks the working set before the (more expensive) grouping step.

Where people lose the point

  • Saying they're interchangeable, or that HAVING is just 'WHERE for GROUP BY' without mentioning aggregates.
  • Putting an aggregate condition in WHERE and expecting it to work.
  • Not knowing the logical clause evaluation order when pressed on why.
Link to this question

2.Explain the difference between INNER, LEFT, RIGHT, and FULL OUTER JOIN. When would you reach for each?

Warm-up

What a strong answer covers

  • INNER returns only matching rows; LEFT keeps every left row and NULL-fills missing right matches; RIGHT is the mirror; FULL OUTER keeps both sides.
  • Concrete use cases: LEFT JOIN for 'all customers including those with zero orders'; INNER for 'only customers who ordered'.
  • Row-count intuition: joins can multiply rows when the join key isn't unique on one side, a classic silent-bug source.
  • A strong answer sketches a two-table example and states what each join returns for it.

Where people lose the point

  • Only defining the joins without a use case for choosing one.
  • Missing that a one-to-many join duplicates rows and inflates downstream aggregates.
  • Confusing which side LEFT preserves.
Link to this question

3.What are window functions? Write a query giving each employee's salary rank within their department.

Core

What a strong answer covers

  • Window functions compute across a set of related rows without collapsing them the way GROUP BY does, every input row survives.
  • Anatomy: function OVER (PARTITION BY ... ORDER BY ...); PARTITION resets the window, ORDER defines ranking/frame order.
  • Example: SELECT name, dept, salary, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) FROM employees;
  • Knows RANK vs DENSE_RANK vs ROW_NUMBER tie-handling differences.

Where people lose the point

  • Reaching for GROUP BY + self-join when a window function is the direct answer.
  • Forgetting PARTITION BY so the rank is global instead of per-department.
  • Not knowing how ties behave across RANK/DENSE_RANK/ROW_NUMBER.
Link to this question

4.Find the second-highest salary in a table, and make it robust to duplicates and to there being no second salary.

Core

What a strong answer covers

  • Clean approach: SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1, DISTINCT handles duplicate top salaries.
  • Alternative: MAX(salary) WHERE salary < (SELECT MAX(salary) ...), or DENSE_RANK() = 2 in a subquery for 'nth' generality.
  • Robustness: returns NULL/empty when only one distinct salary exists, a strong answer says what happens and how the caller should handle it.
  • Generalizes the pattern to nth-highest and mentions the trade-offs between the approaches.

Where people lose the point

  • LIMIT 1 OFFSET 1 without DISTINCT, breaks when the top salary is duplicated.
  • Not addressing the empty/one-row edge case at all.
  • Hardcoding for 'second' with no idea how to generalize to nth.
Link to this question

5.A production query has become slow. Walk me through how you'd diagnose and fix it.

Hard

What a strong answer covers

  • Start with evidence: EXPLAIN/EXPLAIN ANALYZE to see the actual plan, scans vs index use, join strategy, row estimates vs reality.
  • Common culprits: missing/unused indexes, functions wrapping indexed columns, leading-wildcard LIKE, SELECT *, stale statistics, exploding joins.
  • Fix ladder: right index (including composite/covering), query rewrite, then schema-level answers (denormalization, materialized views, partitioning).
  • Mentions verifying the fix under production-like data volume, and that adding an index costs write performance.

Where people lose the point

  • Jumping straight to 'add an index' without reading a query plan first.
  • No mention of EXPLAIN at all.
  • Ignoring that recent data growth or plan changes, not the query text, are often what changed.
Link to this question
No account needed

Answer one real SQL question now

A question a SQL panel actually asks, answered out loud, scored on what you said and how you said it. Under two minutes, and nothing to sign up for.

What is the difference between WHERE and HAVING?

We never store the audio. Your answer is deleted within 24 hours unless you save the result.

How SQL answers get judged

The weights a SQL interviewer is holding, whether or not they say so out loud. Round Zero scores your practice answers against exactly these, and quotes your own words back as the evidence for each.

Correctness

40%

Produces queries and explanations that are logically correct, including edge cases like NULLs and duplicates.

Conceptual depth

30%

Explains why, how the engine evaluates clauses, when indexes help, what a window function does under the hood.

Communication

30%

Walks through reasoning clearly, states assumptions, and structures the answer before diving in.

Role tracks that include SQL

Related Data skills

All skills →

Now say them out loud

You have read what strong SQL answers contain. The next thing that moves the needle is producing one under time, out loud, and finding out where it falls apart.

  • These questions asked back, with follow-ups
  • Flashcards for the ones you keep missing
  • A scored mock that quotes your own answers

Browse every skill

Practising SQL: common questions

What SQL interview questions should I practice?
Start with the core areas SQL interviewers probe: What is the difference between WHERE and HAVING; Explain the difference between INNER, LEFT, RIGHT, and FULL OUTER JOIN. When would you reach for each; What are window functions? Write a query giving each employee's salary rank within their department.. This page outlines strong answers and common mistakes, and the scored path drills each one with follow-ups.
Is the SQL practice free?
Yes. The SQL path runs free inside Round Zero: lessons, practice questions and flashcards. Drills are unlimited on every plan, free included. So is the full scorecard. Free also covers 3 complete scored interviews, no card.
How is this different from a SQL question list?
A static list gives you questions with no feedback. Round Zero runs a live scored practice that probes your actual answers, rotates difficulty, and tells you exactly what to fix, grounded in a SQL rubric.
How should I prepare for a SQL interview?
Learn the concepts, drill the questions until answers come fast, then prove it in a scored mock. Round Zero sequences all three so you know you are ready, not just that you read about SQL.
How is a SQL answer scored?
SQL answers are scored on correctness, conceptual depth, communication, with evidence quoted from what you actually said, so feedback is specific instead of generic praise.