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
- Find customers who never placed an order (LEFT JOIN + IS NULL).
- Given two tables, return rows in A but not in B (EXCEPT, or LEFT JOIN + IS NULL).
- Self-join: for each employee, show their manager's name.
- Explain the difference between UNION and UNION ALL — when is UNION dangerous?
Aggregations & grouping
- Second-highest salary (with and without window functions).
- Departments where the average salary exceeds the company-wide average (HAVING + subquery).
- Top 3 products by revenue per category (ROW_NUMBER over PARTITION).
- Count customers who ordered in every month of Q1 (COUNT DISTINCT + HAVING).
Window functions — the differentiator
- Running total of sales per region ordered by date.
- Month-over-month growth using LAG.
- Rank vs Dense_Rank vs Row_Number — when to use each.
- First and last purchase date per customer using FIRST_VALUE / LAST_VALUE.
- Sessionisation: mark events as belonging to a new session when gap > 30 min.
Dates, strings, and NULLs
- Users who signed up in the last 30 days but haven't logged in in the last 7.
- Extract domain from an email column and count users per domain.
- Handle NULLs safely in SUM, AVG, and joins — where do NULLs silently break things?
- Convert a stored UTC timestamp to IST at query time.
Real-world scenario questions
- Detect duplicate rows and delete keeping the earliest — using CTE + ROW_NUMBER.
- Given orders and refunds, compute net revenue per day.
- Cohort retention: for users who signed up in Jan, what % were active in month 2, 3, 4?
- Funnel: for each user, did they hit event A → B → C in order within 7 days?
- Slowly changing dimension type 2 — write a query to close out old rows and insert new ones.
Performance & internals
- When would an index hurt query performance?
- Difference between clustered and non-clustered index — and how many clustered per table?
- Explain a query plan: what do 'seek' vs 'scan' mean and which is better and when?
- 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