SQL ROW_NUMBER, RANK and DENSE_RANK practice problems
Top N per group, the first order of each customer, the second highest salary with ties: all of these are ranking problems. ROW_NUMBER gives every row a different number, RANK leaves gaps after ties and DENSE_RANK does not.
Choosing the wrong one of the three is the classic mistake, and the hidden data in these problems contains ties on purpose, so the difference shows.
Try one here: Rank within department
Try it yourself
Return each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
employees (11 rows)
| id | name | dept_id | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Ava | 2 | 130000 | 2019-03-15 | NULL |
| 2 | Ben | 2 | 105000 | 2020-07-01 | 1 |
| 3 | Cleo | 1 | 82000 | 2021-01-10 | 7 |
| 4 | Dan | 1 | 76000 | 2022-05-23 | 7 |
| 5 | Eve | 3 | 54000 | 2022-09-12 | 11 |
| 6 | Finn | 2 | 98000 | 2021-11-30 | 1 |
| 7 | Gia | 1 | 99000 | 2018-06-04 | NULL |
| 8 | Hugo | 3 | 61000 | 2020-02-18 | NULL |
| 9 | Iris | 4 | 88000 | 2019-10-07 | NULL |
| 10 | Jack | 4 | 72000 | 2023-04-17 | 9 |
| 11 | Kai | NULL | 50000 | 2024-01-08 | 8 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
Advanced
- Rank within departmentReturn each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
- Top earner per departmentReturn the dept_id, name and salary of the highest-paid employee in each department. If several employees share the top salary in a department, return all of them. Ignore employees without a department.
- Dense rankReturn every employee's name, salary and DENSE_RANK of the salary across the whole company, where 1 is the highest.
- Each customer's first orderFor each customer, return the customer_id and the id of their first order (earliest date, then lowest id). Count every order, whatever the status.
- Top two per categoryFor each category, return the category, product name and revenue (quantity times price) of its two best-selling products by revenue.
- Salary quartilesSplit the employees into four groups of as equal size as possible by salary, highest salaries in group 1. Return each employee's name and their group number.
- Most orders, ties includedWhich customers placed the most orders, counting orders of every status? Return the name and the number of orders of every customer who shares first place. Customer names are unique.
Interview classics
- Second highest salary per departmentFor each department with at least two different salaries, return the dept_id and its second highest salary. Ignore employees without a department.
- Median salaryReturn the median salary of all employees as a single value: the middle salary when they are sorted, or the average of the two middle salaries when the number of employees is even.
- Runs of the same statusGo through the orders by id and group consecutive orders that share the same status into runs. For each run, return the status, the first order id, the last order id and the number of orders in the run.
- First order not paidReturn the names of customers whose first order (earliest date, then lowest id) has a status other than paid.
- Longest streak of active daysReturn each user_id with the length of their longest run of consecutive active days. A user who was never active two days in a row has a streak of 1.
Work mode
- Keep one account per personFrom Sam in Support: "For everyone who signed up more than once with the same email typed differently, I only want their earliest signup. The signup dates come in mixed formats, and if two signups fall on the same day the lowest id wins. Send me one row per person." Return the id of the account to keep and the email in lowercase with no spaces around it.
Case studies
- Product performance, step 3: best seller in each categoryFor each category, return category, name and revenue of the product with the highest paid revenue in that category.
Read first if you are stuck
Other practice sets
Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.