How to Use ORDER BY in SQL
Use ORDER BY when query results should appear in a specific sequence instead of the database’s default order. This is essential for sorting dates, prices, names, scores, and ranked reports.
What you’ll build or solve
You’ll learn how to use ORDER BY in SQL for ascending and descending sorting. You’ll also know how to sort by multiple columns and aggregated results.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when the result order affects readability, rankings, or business logic.
Common real-world scenarios include:
- Newest orders first
- Highest prices first
- Alphabetical customer lists
- Top scores
- Revenue leaderboards
This is a bad idea when result order does not matter and sorting would only add unnecessary query cost.
Prerequisites
You only need:
- A SQL table with sortable columns
- Basic
SELECTknowledge
Step-by-step instructions
Step 1: Add ORDER BY after the main query
Use ascending order by default.
SQL
SELECT *
FROM products
ORDER BY price;
This sorts from lowest to highest.
Use DESC for descending order.
SQL
SELECT *
FROM orders
ORDER BY created_at DESC;
This is perfect for newest-first lists.
Sort by multiple columns when tie-breaking matters.
SQL
SELECT *
FROM users
ORDER BY country, created_at DESC;
What to look for:
- Ascending is the default
- Use
DESCfor highest/newest first - Multiple columns create tie-breakers
- Great for rankings and reports
- Sorting large datasets can add cost
Examples you can copy
Newest users
SQL
SELECT *
FROM users
ORDER BY created_at DESC;
Cheapest products
SQL
SELECT *
FROM products
ORDER BY price ASC;
Revenue leaderboard
SQL
SELECT country, SUM(total) AS revenue
FROM orders
GROUP BY country
ORDER BY revenue DESC;
Common mistakes and how to fix them
Mistake 1: Expecting DESC by default
What the reader might do:
SQL
ORDER BY created_at
Why it breaks: this sorts oldest first.
Corrected approach:
SQL
ORDER BY created_at DESC
Mistake 2: Sorting before grouping logic
What the reader might do:
SQL
SELECT country, SUM(total)
FROM orders
ORDER BY SUM(total) DESC
GROUP BY country;
Why it breaks: GROUP BY must come before ORDER BY.
Corrected approach:
Place sorting after grouping.
Mistake 3: Sorting by ambiguous column names in joins
What the reader might do:
SQL
ORDER BY created_at
Why it breaks: multiple joined tables may contain the same column.
Corrected approach:
Use the table alias.
SQL
ORDER BY orders.created_at DESC
Troubleshooting
If the order looks reversed, switch ASC and DESC.
If grouped queries error, place ORDER BY after GROUP BY.
If ties look random, add a second sorting column.
If the query slows down, sort fewer rows or add indexes.
Quick recap
- Use
ORDER BYto sort results - Ascending is default
- Use
DESCfor highest/newest first - Add multiple columns for tie-breakers
- Place it after grouping logic
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot