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_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 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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business 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_no | first_name | gender | salary |
|---|---|---|---|
| 1024 | Anthony | M | 56794 |
| 1015 | Charles | M | 57226 |
| 1019 | Christopher | M | 59341 |
| 1020 | Daniel | M | 44012 |
| 1007 | David | M | 39400 |
| 1001 | James | M | 45300 |
| 1030 | Joe | M | 44000 |
| 1004 | John | M | 36264 |
| 1012 | Joseph | M | 60556 |
| 1025 | Mark | M | 65120 |
| 1023 | Matthew | M | 41365 |
| 1006 | Michael | M | 23800 |
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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
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_no | first_name | department | birth_date |
|---|---|---|---|
| 1027 | Barbara | HR | 1999-12-27 |
| 1006 | Michael | HR | 1999-04-16 |
| 1004 | John | HR | 1999-02-01 |
| 1018 | Betty | HR | 1998-11-13 |
| 1016 | Jessica | IT | 2000-01-25 |
| 1020 | Daniel | IT | 1999-01-21 |
| 1002 | Robert | IT | 1998-09-02 |
| 1023 | Matthew | IT | 1998-05-16 |
| 1007 | David | IT | 1998-04-26 |
| 1021 | David | IT | 1998-01-24 |
| 1028 | Jessica | IT | 1998-01-24 |
| 1014 | Thomas | IT | 1996-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.
