SQL interview questions
12 classic SQL interview questions you can run in the browser. Each one has a small dataset, a checker that compares your output with the expected result, hints, and an explained solution. They cover joins, GROUP BY and HAVING, subqueries, and window functions such as RANK, DENSE_RANK, ROW_NUMBER and LAG. No login, nothing to install.
1. Second highest salary
Find the second highest distinct salary, or NULL when there isn't one.
2. Duplicate emails
List every email address that appears more than once.
3. Employees earning more than their manager
Self-join the employees table to compare each person with their manager.
4. Customers who never ordered
Find rows in one table with no match in another (anti-join).
5. Highest salary in each department
Return the top earner(s) in every department, including ties.
6. Top three salaries per department
Use DENSE_RANK to keep everyone in the top three distinct salaries of each department.
7. Departments paid above the company average
Compare a group average with an overall average.
8. Rank scores without gaps
Dense-rank scores from highest to lowest, with ties sharing a rank.
9. Running total of daily revenue
Aggregate per day, then use SUM() OVER for a cumulative total.
10. Month-over-month revenue growth
Use LAG() to compare each month with the previous one.
11. Users who logged in three days in a row
Solve a gaps-and-islands problem with ROW_NUMBER().
12. Each customer's first order
Pick the earliest row per group with ROW_NUMBER().