Sorting Results with ORDER BY
Why Sort Your Data?
The Need for Order
By default, database results are often returned in an unpredictable order. To make data readable for analysis, we use the ORDER BY clause.
Imagine looking for a specific movie in a list of thousands that isn't sorted—it would be nearly impossible!
Welcome! When you query a database, the results might look like this—a jumbled mess of movies. To turn this chaos into a clean, organized list, we use the ORDER BY clause.
- Database results are naturally unordered.
- ORDER BY organizes data for better readability.
- Sorting is essential for finding specific records quickly.
The ORDER BY Syntax
Syntax Template
The ORDER BY keyword is placed at the very end of your SQL statement.
SELECT column1, column2
FROM table_name
ORDER BY column_name [ASC|DESC];Key Rules:
- ASC: Lowest to highest (Default).
- DESC: Highest to lowest.
Let's look at the syntax. You start with your SELECT and FROM clauses, then add ORDER BY followed by the column you want to sort. Remember, ASC is the default, so you don't always have to type it.
- ORDER BY goes at the end of the query.
- ASC is the default behavior.
- DESC must be explicitly stated for reverse order.
Sorting Text: Alphabetical Order
Alphabetical Sort (ASC)
To see all movies listed alphabetically by their title, we sort the Title column.
Click the button to run the query and see how the Movie table reacts.
Let's try an alphabetical sort. Here is our query to select Title and Year, ordered by Title. Click 'Run Query' to see the result. Great! Notice how the results now start with 'Interstellar' and end with 'Toy Story'. This is the default Ascending behavior.
- Sorting text columns results in A-Z order by default.
- The query uses ORDER BY Title.
Sorting Numbers: Chronological Order
Chronological Sort (DESC)
To find the most recent movies first, we sort the Year column in descending order using the DESC keyword.
What if you want the newest movies first? We add the DESC keyword after the Year column. Give it a try. Perfect. By using DESC, the year 2014 comes first, moving down to 1972 at the bottom.
- DESC reverses the order (highest numbers first).
- Useful for dates and years.
The Golden Rule of Clause Order
Where does ORDER BY go?
SQL clauses must follow a strict sequence. If you are filtering data with a WHERE clause, it must come BEFORE the ORDER BY clause.
There is one golden rule you must remember. In SQL, the order of clauses is fixed. SELECT and FROM always come first, followed by WHERE for filtering, and finally ORDER BY for sorting. If you swap WHERE and ORDER BY, your query will break.
- Sequence: SELECT -> FROM -> WHERE -> ORDER BY.
- Placing ORDER BY before WHERE results in a syntax error.
Hands-on Challenge
Your Turn
Write a query to retrieve the Title and Genre of all movies, sorted alphabetically by Genre.
It's time for your first coding challenge. Write a query to select the Title and Genre, then sort them by the Genre column. Click submit when you're ready.
- Apply SELECT and FROM.
- Apply ORDER BY to the Genre column.