SQL ORDER BY
Anonymous contributor
Published Jun 4, 2021Updated Oct 6, 2023
The ORDER BY command sorts the result set by a particular column either alphabetically or numerically.
Syntax
SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC;
It’s also possible to ORDER BY multiple columns by separating them with a comma.
SELECT column_name_1
FROM table_name
ORDER BY column_name_1 ASC | DESC, column_name_2 ASC | DESC;
This assumes column_name_1 and column_name_2 exist in table_name.
ORDER BY can be set in two ways:
ASCis a keyword used to sort the results in ascending order (default).DESCis a keyword used to sort the results in descending order.
Example
The query below will sort by birth_date, in descending order:
SELECT *FROM contactsORDER BY birth_date DESC;
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.18 hours
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.5 hours