A window function calculates a value for each row while looking at a "window" of related rows, without collapsing them like GROUP BY does. ROW_NUMBER() OVER (...) gives each row a number. PARTITION BY restarts the numbering for each group, and ORDER BY inside OVER decides who gets number 1.
The most common use is "top N per group": number the rows in a subquery, then keep the rows where the number is small. You must filter in an outer query, because a window function cannot be used directly in WHERE.
Window functions need MySQL 8.0 or newer (MariaDB 10.2 or newer).
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY group_col ORDER BY sort_col DESC) AS rn
FROM table_name
) t
WHERE rn = 1;
These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.
See the employees table
Table: employees
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Highest paid employee per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL | 1 |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development | 1 |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance | 1 |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR | 1 |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT | 1 |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing | 1 |
Employees with no department form their own group, shown here with department NULL. In descending order, NULL salaries come last.
Question 2Second highest salary per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn = 2;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL | 2 |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development | 2 |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance | 2 |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR | 2 |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT | 2 |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing | 2 |
If two people share a salary, ROW_NUMBER still gives them different numbers. Use DENSE_RANK when ties should count as the same rank.
Question 3Top 2 employees per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn <= 2;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL | 1 |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL | 2 |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development | 1 |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development | 2 |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance | 1 |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance | 2 |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR | 1 |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR | 2 |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT | 1 |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT | 2 |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing | 1 |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing | 2 |
Question 4Lowest paid employee per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary ASC) AS rn
FROM employees
WHERE salary IS NOT NULL
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL | 1 |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development | 1 |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance | 1 |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR | 1 |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT | 1 |
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing | 1 |
Different from the PDF: The PDF answer did not filter out NULL salaries. In ascending order MySQL puts NULL first, so a person with no salary was shown as the "lowest paid". WHERE salary IS NOT NULL fixes it.
Question 5Most recently joined employee per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY joining_date DESC) AS rn
FROM employees
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL | 1 |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development | 1 |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance | 1 |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR | 1 |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT | 1 |
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing | 1 |
Question 6First joined employee per department.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY joining_date ASC) AS rn
FROM employees
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL | 1 |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development | 1 |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance | 1 |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR | 1 |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT | 1 |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing | 1 |
Question 7Top 3 highest paid employees in the company.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn <= 3;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT | 1 |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance | 2 |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development | 3 |
Different from the PDF: The PDF answer partitioned by department, which gives the top 3 in every department. The question asks for the top 3 in the whole company, so the PARTITION BY is removed.
Question 8List the employees sorted by salary with a row number.
Show the answer and the output
SELECT *,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM employees;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT | 1 |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance | 2 |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development | 3 |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT | 4 |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance | 5 |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing | 6 |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing | 7 |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development | 8 |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL | 9 |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development | 10 |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance | 11 |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance | 12 |
Showing the first 12 of 30 rows.
Question 9Remove duplicate names: keep only one record per first name, the one with the latest joining date.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY first_name ORDER BY joining_date DESC) AS rn
FROM employees
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development | 1 |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR | 1 |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR | 1 |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL | 1 |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing | 1 |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT | 1 |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT | 1 |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL | 1 |
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing | 1 |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing | 1 |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT | 1 |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development | 1 |
Showing the first 12 of 26 rows.
This query selects the rows to keep. To delete the others, you would delete the rows where rn > 1.
Question 10Highest paid employee in the IT department only.
Show the answer and the output
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM employees
WHERE department = 'IT'
) t
WHERE rn = 1;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department | rn |
|---|---|---|---|---|---|---|---|---|
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT | 1 |
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
