What & Why
The ORDER BY clause is used to sort the result set by one or more columns, ascending (ASC) or descending (DESC). ORDER BY turns a raw result into a useful list. Analysts use it for top campaigns, newest accounts, lowest ratings, and stable exports.
See how it works
Marketing wants campaigns ranked by spend from highest to lowest.
SELECT
name,
channel,
spend
FROM marketing.campaigns
ORDER BY spend DESC, name ASC;
Syntax pattern
See explanation
ORDER BY column_name ASC
ORDER BY metric DESC, name ASC
- ASC sorts smallest to largest; DESC sorts largest to smallest.
- Use secondary sort columns for stable tie-breaking.
- ORDER BY runs near the end of the query.
Now You Try
Practice this concept hands-on
Sign up for a free account to run SQL against real datasets right here in the browser and get instant AI feedback. No credit card required.
Sign up free