CodeEraFree roadmap

SQL Practice / ORDER BY clause

SQL ORDER BY Clause: practice questions with answers

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

ORDER BY sorts the result. The default is ascending (ASC); add DESC for descending. You can sort by several columns: the second column only breaks ties in the first. ORDER BY always comes after WHERE.

In MySQL, NULL values come first in ascending order and last in descending order.

SELECT columns FROM table_name
WHERE condition
ORDER BY column1 ASC, column2 DESC;

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 1Retrieve all the records sorted by joining_date in descending order.

Show the answer and the output
SELECT * FROM employees ORDER BY joining_date DESC;

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1020DanielElliott440121999-01-21M2023-01-08IT
1006MichaelWatts238001999-04-16M2022-06-09HR
1014ThomasRyan236761996-10-12M2022-03-02IT
1017NancyParker669001996-10-24F2022-01-09Finance
1007DavidMills394001998-04-26M2021-10-17IT
1009LindaPotter552311998-01-24F2021-08-07Finance
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1028JessicaClark638361998-01-24F2021-05-23IT
1013MaryGill582461999-01-21F2021-04-30Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1010RichardMartin256001996-08-07M2021-03-23IT
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR

Showing the first 12 of 30 rows.

Question 2Retrieve all records from the employees table sorted by last name in ascending order.

Show the answer and the output
SELECT * FROM employees ORDER BY last_name ASC;

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1018BettyBarker352881998-11-13F2019-12-26HR
1001JamesBond453001998-01-19M2020-11-29Marketing
1030JoeBrooks440001999-08-05M2021-03-09Business Development
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1004JohnCole362641999-02-01M2021-04-14HR
1020DanielElliott440121999-01-21M2023-01-08IT
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1005JenniferGill489411996-06-05F2019-06-28Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development

Showing the first 12 of 30 rows.

Question 3Retrieve emp_no, first_name, gender and salary of employees whose salary is not NULL, sorted by gender ascending and then by first name ascending.

Show the answer and the output
SELECT emp_no, first_name, gender, salary
FROM employees
WHERE salary IS NOT NULL
ORDER BY gender, first_name;

Output

emp_nofirst_namegendersalary
1024AnthonyM56794
1015CharlesM57226
1019ChristopherM59341
1020DanielM44012
1007DavidM39400
1001JamesM45300
1030JoeM44000
1004JohnM36264
1012JosephM60556
1025MarkM65120
1023MatthewM41365
1006MichaelM23800

Showing the first 12 of 27 rows.

Question 4Get the employees whose emp_no is between 1011 and 1020, sorted by emp_no in descending order.

Show the answer and the output
SELECT * FROM employees WHERE emp_no BETWEEN 1011 AND 1020 ORDER BY emp_no DESC;

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1020DanielElliott440121999-01-21M2023-01-08IT
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1018BettyBarker352881998-11-13F2019-12-26HR
1017NancyParker669001996-10-24F2022-01-09Finance
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1014ThomasRyan236761996-10-12M2022-03-02IT
1013MaryGill582461999-01-21F2021-04-30Business Development
1012JosephTurner605561997-05-13M2019-09-02Marketing
1011SusanHall541521999-12-27F2020-08-07Finance

Question 5Retrieve the unique first names sorted in ascending order.

Show the answer and the output
SELECT DISTINCT first_name FROM employees ORDER BY first_name;

Output

first_name
Anthony
Barbara
Betty
Charles
Christopher
Daniel
David
Elizabeth
James
Jennifer
Jessica
Joe

Showing the first 12 of 26 rows.

Question 6Retrieve emp_no, first_name, department and birth_date of employees whose department has only two characters, sorted by department ascending and then by birth_date descending.

Show the answer and the output
SELECT emp_no, first_name, department, birth_date
FROM employees
WHERE department LIKE '__'
ORDER BY department, birth_date DESC;

Output

emp_nofirst_namedepartmentbirth_date
1027BarbaraHR1999-12-27
1006MichaelHR1999-04-16
1004JohnHR1999-02-01
1018BettyHR1998-11-13
1016JessicaIT2000-01-25
1020DanielIT1999-01-21
1002RobertIT1998-09-02
1023MatthewIT1998-05-16
1007DavidIT1998-04-26
1021DavidIT1998-01-24
1028JessicaIT1998-01-24
1014ThomasIT1996-10-12

Showing the first 12 of 13 rows.

'__' is two underscores: exactly two characters. In this data only 'HR' and 'IT' match.

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