What is ORDER BY?
The ORDER BY clause lets you sort query results based on one or more columns. Without it, rows are returned in an unpredictable order.
Think of it like sorting products on Amazon by price or rating. ORDER BY does the same for your database queries, making data easier to analyze and present.
Basic Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column_name [ASC|DESC];💡 Pro Tip: ORDER BY is the last clause in a SELECT statement (after WHERE and GROUP BY). ASC (ascending) is the default and can be omitted.
Common Patterns
Sort results from lowest to highest (default)
SELECT name, age
FROM customers
ORDER BY age ASC;Result: Shows youngest customers first (18, 25, 30, 45...)
See Sorting In Action
Watch how ORDER BY transforms data! See the before and after states to understand how sorting works.
Sort by Name (A-Z)
Alphabetical ordering
Input Data
idINTEGER | nameTEXT | emailTEXT | countryTEXT | ageINTEGER |
|---|---|---|---|---|
| 1 | Alice Johnson | [email protected] | USA | 28 |
| 2 | Bob Smith | [email protected] | Canada | 35 |
| 3 | Carlos Silva | [email protected] | Brazil | 22 |
| 4 | Diana Chen | [email protected] | USA | 41 |
| 5 | Emma Davis | [email protected] | Canada | 19 |
SQL Query
SELECT name, country
FROM customers
ORDER BY name ASCResult
5 rowsnameTEXT | countryTEXT |
|---|---|
| Alice Johnson | USA |
| Bob Smith | Canada |
| Carlos Silva | Brazil |
| Diana Chen | USA |
| Emma Davis | Canada |
Key Concepts
Ascending Order
Sorts from smallest to largest (A→Z, 0→9, oldest→newest).
ORDER BY price ASCThis is the default - you can omit ASC
Descending Order
Sorts from largest to smallest (Z→A, 9→0, newest→oldest).
ORDER BY price DESCMust explicitly specify DESC
Multiple Columns
Sort by first column, then use second column to break ties.
ORDER BY country, age DESCCan mix ASC and DESC
NULL Handling
NULL values typically appear last in ASC, first in DESC.
ORDER BY last_login DESCBehavior varies by database
Sorting Different Data Types
Numbers
Sorted numerically: 1, 2, 10, 20, 100 (not alphabetically)
SELECT * FROM products ORDER BY price DESC;Text (Strings)
Sorted alphabetically. Case-sensitivity depends on database settings.
SELECT * FROM customers ORDER BY name ASC;Dates & Times
Sorted chronologically. Older dates come first in ASC.
SELECT * FROM orders ORDER BY order_date DESC; -- Most recent firstBooleans
False (0) before True (1) in ASC order.
SELECT * FROM tasks ORDER BY completed ASC; -- Incomplete tasks firstCommon Mistakes to Avoid
❌ Ordering by column not in SELECT
SELECT name FROM customers ORDER BY age;While often allowed, it's confusing. Better to include it: SELECT name, age FROM customers ORDER BY age
❌ Forgetting DESC for reverse order
SELECT * FROM products ORDER BY price; -- Shows cheapest first!Default is ASC. For expensive-first: ORDER BY price DESC
❌ Wrong order in multi-column sort
ORDER BY age, country -- Groups by age, then countryOrder matters! For country grouping: ORDER BY country, age
❌ Using ORDER BY before WHERE
SELECT * FROM customers ORDER BY age WHERE country = 'USA';Wrong clause order! Correct: WHERE country = 'USA' ORDER BY age
✅ Correct multi-column sort with mixed directions
SELECT name, country, age FROM customers WHERE age >= 18 ORDER BY country ASC, age DESC;Perfect! Groups by country alphabetically, oldest first within each.