ORDER BY Multiple Columns in SQL

Level 380 keystrokes

ORDER BY with several columns sorts by the first column, then by the next one when values are equal.

SQL code

1SELECT
-- Returns the name, department and salary columns
2 name,
3 department,
4 salary
5FROM
6 employees
-- Sorts by several columns, from left to right
7ORDER BY
-- First by department, ascending (ASC is the default)
8 department,
-- Then, within the same department, by salary from highest to lowest
9 salary DESC;

Output

+-------+------------+--------+
| name  | department | salary |
+-------+------------+--------+
| Bob   | HR         |  55000 |
| Alice | HR         |  50000 |
| Carol | IT         |  75000 |
| Dan   | IT         |  65000 |
+-------+------------+--------+
4 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • ,× 3
  • ;× 1

More SQL examples at level 3

← SQL typing practice