SpinTheMic
Pro

5 guides

How to answer SQL interview questions

SQL questions look simple, so interviewers judge you on precision: the exact output of RANK on a tie, what NOT IN does with a NULL, why an alias fails in WHERE. These guides give you a clear spoken answer and the small example that proves you understand it.

  1. What is the difference between WHERE and HAVING in SQL?WHERE filters rows before they are grouped, so it can't use aggregates. HAVING filters groups after GROUP BY, so it can, for example HAVING COUNT(*) > 5.Beginner
  2. In what order is a SQL query executed?Logically, a query runs FROM and JOIN, then WHERE, GROUP BY, HAVING, then SELECT (with window functions), DISTINCT, ORDER BY and finally LIMIT. The engine may physically do things differently, but results must match this order.Beginner
  3. What is the difference between UNION and UNION ALL?Both stack the results of two queries. UNION removes duplicate rows, which costs extra work; UNION ALL keeps every row and is faster. Both need the same number of columns with compatible types.Beginner
  4. What is the difference between ROW_NUMBER, RANK and DENSE_RANK?They differ only on ties. For scores 100, 90, 90, 80: ROW_NUMBER gives 1, 2, 3, 4; RANK gives 1, 2, 2, 4, leaving a gap; DENSE_RANK gives 1, 2, 2, 3, with no gap.Intermediate
  5. What is the difference between EXISTS, IN and NOT IN?IN checks a value against a list or subquery; EXISTS checks whether a subquery returns any row. Their results usually match. NOT IN is the trap: if the subquery returns a NULL, NOT IN returns no rows, so NOT EXISTS is the safer choice.Intermediate
All SQL practice questions