All resources
Interview QuestionsJul 16, 20269 min read

SQL Interview Questions: The 25 That Actually Get Asked

There are hundreds of SQL interview questions online. In practice, twenty-five patterns cover almost every real loop — analyst, engineer, or PM. Master these, in depth, and you'll walk into any SQL round already ahead.

Joins & set operations

  1. Find customers who never placed an order (LEFT JOIN + IS NULL).
  2. Given two tables, return rows in A but not in B (EXCEPT, or LEFT JOIN + IS NULL).
  3. Self-join: for each employee, show their manager's name.
  4. Explain the difference between UNION and UNION ALL — when is UNION dangerous?

Aggregations & grouping

  1. Second-highest salary (with and without window functions).
  2. Departments where the average salary exceeds the company-wide average (HAVING + subquery).
  3. Top 3 products by revenue per category (ROW_NUMBER over PARTITION).
  4. Count customers who ordered in every month of Q1 (COUNT DISTINCT + HAVING).

Window functions — the differentiator

  1. Running total of sales per region ordered by date.
  2. Month-over-month growth using LAG.
  3. Rank vs Dense_Rank vs Row_Number — when to use each.
  4. First and last purchase date per customer using FIRST_VALUE / LAST_VALUE.
  5. Sessionisation: mark events as belonging to a new session when gap > 30 min.

Dates, strings, and NULLs

  1. Users who signed up in the last 30 days but haven't logged in in the last 7.
  2. Extract domain from an email column and count users per domain.
  3. Handle NULLs safely in SUM, AVG, and joins — where do NULLs silently break things?
  4. Convert a stored UTC timestamp to IST at query time.

Real-world scenario questions

  1. Detect duplicate rows and delete keeping the earliest — using CTE + ROW_NUMBER.
  2. Given orders and refunds, compute net revenue per day.
  3. Cohort retention: for users who signed up in Jan, what % were active in month 2, 3, 4?
  4. Funnel: for each user, did they hit event A → B → C in order within 7 days?
  5. Slowly changing dimension type 2 — write a query to close out old rows and insert new ones.

Performance & internals

  1. When would an index hurt query performance?
  2. Difference between clustered and non-clustered index — and how many clustered per table?
  3. Explain a query plan: what do 'seek' vs 'scan' mean and which is better and when?
  4. How does the optimiser decide join order — and how do you influence it safely?

Interviewers score structure as much as syntax. Talk through your query as you write: 'I'm partitioning by user_id, ordering by created_at, and taking the first row.' A silent correct query scores lower than a narrated one.

Recommended next action

Take the next concrete step — it's free, takes under a minute, and gives you a real score to act on.

Practice SQL with an AI interviewer

Keep reading