SQL Interview Practice
14 hand-picked problems with multiple solutions, side-by-side explanations, and a one-click in-browser playground for each.
Find the second highest salary
Given an Employee table, write a query to return the second highest salary. If there is no second highest salary, return NULL.
Employees earning more than their manager
Given an Employee table where ManagerID points to another EmployeeID in the same table, find all employees who earn more than their direct manager.
JOIN types — INNER, LEFT, RIGHT, FULL OUTER
Two tables: Orders and Customers. Demonstrate each JOIN type. Note: sql.js does not support RIGHT JOIN or FULL OUTER JOIN directly — use UNION simulations.
GROUP BY with HAVING
Find products with total sales greater than 1000. Demonstrate the difference between WHERE (row filter) and HAVING (group filter).
Window functions — ROW_NUMBER, RANK, DENSE_RANK
Show three ways to rank employees by salary within each department. Notice how they behave differently when there are ties.
Running total with SUM OVER
Compute a cumulative running total of sales by date.
UNION vs UNION ALL
Combine sales from 2023 and 2024. Show the difference between UNION (dedup) and UNION ALL (keep all).
Find duplicate records
Find customers with duplicate email addresses, then show how to keep only the most recent.
Top N customers by revenue
Find the top 5 customers by total revenue along with their rank. Practice the multi-step approach with CTEs.
Recursive CTE — employee hierarchy
Find all direct and indirect reports for a manager. Standard hierarchy traversal — a common Hard interview problem.
Find gaps in sequential data
Given order IDs 1, 2, 3, 5, 6, 8, 9 — identify the missing IDs (4 and 7). Useful for finding deleted records or audit gaps.
Month-over-month growth %
Compute month-over-month sales growth as a percentage. Standard BI / analytics interview question.
WHERE vs HAVING
Demonstrate the execution order and difference between WHERE (row filter, before grouping) and HAVING (group filter, after grouping).
Fact table validation queries
Run a battery of data quality checks on a fact table — NULL foreign keys, orphaned rows, negative amounts, future dates.