Basics · Lesson 5 of 19

Sorting with ORDER BY

ORDER BY sorts your results.

Ascending (smallest to largest) - default:

SELECT * FROM employees ORDER BY salary;

Descending (largest to smallest):

SELECT * FROM employees ORDER BY salary DESC;

Sort by multiple columns:

ORDER BY department_id, salary DESC

This sorts by department first, then by salary within each department.

Check your understanding

List all employees sorted by salary from highest to lowest.

Examples run on the employees sample database. Open any of them in the playground to experiment.

Practice it on a real question

All SELECT & filtering questions

Practice on your iPhone

Query Dojo for iPhone has 85 interview questions, timed mock interviews and a tile builder, so you can write queries without a keyboard.

Get the app