CodeEraFree roadmap

SQL Practice / Window functions

SQL Window Functions and ROW_NUMBER(): practice questions with answers

10 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business 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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1015CharlesWilliams572262000-01-29M2019-11-05NULL1
1025MarkWood651201998-10-29M2020-07-30Business Development1
1017NancyParker669001996-10-24F2022-01-09Finance1
1004JohnCole362641999-02-01M2021-04-14HR1
1002RobertLee694101998-09-02M2019-12-26IT1
1012JosephTurner605561997-05-13M2019-09-02Marketing1

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1026ElizabethWalker470301998-08-11F2021-06-18NULL2
1013MaryGill582461999-01-21F2021-04-30Business Development2
1029NancyCole636692000-01-26F2019-12-26Finance2
1018BettyBarker352881998-11-13F2019-12-26HR2
1028JessicaClark638361998-01-24F2021-05-23IT2
1019ChristopherGibson593411998-04-23M2019-05-02Marketing2

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1015CharlesWilliams572262000-01-29M2019-11-05NULL1
1026ElizabethWalker470301998-08-11F2021-06-18NULL2
1025MarkWood651201998-10-29M2020-07-30Business Development1
1013MaryGill582461999-01-21F2021-04-30Business Development2
1017NancyParker669001996-10-24F2022-01-09Finance1
1029NancyCole636692000-01-26F2019-12-26Finance2
1004JohnCole362641999-02-01M2021-04-14HR1
1018BettyBarker352881998-11-13F2019-12-26HR2
1002RobertLee694101998-09-02M2019-12-26IT1
1028JessicaClark638361998-01-24F2021-05-23IT2
1012JosephTurner605561997-05-13M2019-09-02Marketing1
1019ChristopherGibson593411998-04-23M2019-05-02Marketing2

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1026ElizabethWalker470301998-08-11F2021-06-18NULL1
1008WilliamHill263921999-10-21M2019-09-30Business Development1
1022SarahHarrison446921996-03-20F2020-05-03Finance1
1006MichaelWatts238001999-04-16M2022-06-09HR1
1014ThomasRyan236761996-10-12M2022-03-02IT1
1001JamesBond453001998-01-19M2020-11-29Marketing1

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1026ElizabethWalker470301998-08-11F2021-06-18NULL1
1013MaryGill582461999-01-21F2021-04-30Business Development1
1017NancyParker669001996-10-24F2022-01-09Finance1
1006MichaelWatts238001999-04-16M2022-06-09HR1
1020DanielElliott440121999-01-21M2023-01-08IT1
1001JamesBond453001998-01-19M2020-11-29Marketing1

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1015CharlesWilliams572262000-01-29M2019-11-05NULL1
1024AnthonyMiller567941996-06-30M2019-02-28Business Development1
1029NancyCole636692000-01-26F2019-12-26Finance1
1018BettyBarker352881998-11-13F2019-12-26HR1
1023MatthewHill413651998-05-16M2019-05-23IT1
1019ChristopherGibson593411998-04-23M2019-05-02Marketing1

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1002RobertLee694101998-09-02M2019-12-26IT1
1017NancyParker669001996-10-24F2022-01-09Finance2
1025MarkWood651201998-10-29M2020-07-30Business Development3

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1002RobertLee694101998-09-02M2019-12-26IT1
1017NancyParker669001996-10-24F2022-01-09Finance2
1025MarkWood651201998-10-29M2020-07-30Business Development3
1028JessicaClark638361998-01-24F2021-05-23IT4
1029NancyCole636692000-01-26F2019-12-26Finance5
1012JosephTurner605561997-05-13M2019-09-02Marketing6
1019ChristopherGibson593411998-04-23M2019-05-02Marketing7
1013MaryGill582461999-01-21F2021-04-30Business Development8
1015CharlesWilliams572262000-01-29M2019-11-05NULL9
1024AnthonyMiller567941996-06-30M2019-02-28Business Development10
1009LindaPotter552311998-01-24F2021-08-07Finance11
1011SusanHall541521999-12-27F2020-08-07Finance12

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1024AnthonyMiller567941996-06-30M2019-02-28Business Development1
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR1
1018BettyBarker352881998-11-13F2019-12-26HR1
1015CharlesWilliams572262000-01-29M2019-11-05NULL1
1019ChristopherGibson593411998-04-23M2019-05-02Marketing1
1020DanielElliott440121999-01-21M2023-01-08IT1
1007DavidMills394001998-04-26M2021-10-17IT1
1026ElizabethWalker470301998-08-11F2021-06-18NULL1
1001JamesBond453001998-01-19M2020-11-29Marketing1
1005JenniferGill489411996-06-05F2019-06-28Marketing1
1028JessicaClark638361998-01-24F2021-05-23IT1
1030JoeBrooks440001999-08-05M2021-03-09Business Development1

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartmentrn
1002RobertLee694101998-09-02M2019-12-26IT1

Want these as PDFs? Download the question PDF or the answer PDF to practise offline.