ORDER BY

Sort results by any column — ascending, descending, or both

Beginner 10 minORDER BYsorting

Sorting Results

Without ORDER BY, SQL makes no guarantee about row order. You might get the same sequence each time, or you might not — it depends on the database engine. If order matters, you must always specify it explicitly. Here's how sorting works:

ASC is the default — Ascending order means smallest first, A→Z for text, and earliest first for dates. Use DESC to reverse the direction: largest first, Z→A, and most recent dates at the top of your results.
Multi-column tiebreakersORDER BY category ASC, price DESC groups rows by category alphabetically, then within each category shows the most expensive products first. Later columns only matter when earlier columns have ties.
Sort by hidden columns — You can ORDER BY a column that isn't in your SELECT list. This is useful when you want sorted results without displaying the sort key itself in the output.
Sort by positionORDER BY 2 DESC sorts by the second column in your SELECT list. Handy for quick queries, though less readable than naming the column explicitly in production code.
NULL sort order is database-specific — SQLite and MySQL place NULLs first in ASC by default; PostgreSQL and Oracle place them last. PostgreSQL and Oracle support ORDER BY col ASC NULLS LAST (or NULLS FIRST) for explicit control. SQLite added the same syntax in 3.30 (Oct 2019). MySQL has no NULLS FIRST/LAST; emulate with ORDER BY col IS NULL, col.
Knowledge Canvas

How ORDER BY Works

Sort your results before display

  • ORDER BY runs LAST — after SELECT
  • ASC = ascending (default), DESC = descending
  • Multi-column sort: first column breaks ties with second
  • Can reference column aliases or positions (1, 2, 3)

NULL Sorting Behavior

Where do NULLs end up?

  • Behavior varies by database — never assume
  • PostgreSQL / Oracle: NULLs sort LAST in ASC
  • MySQL / SQLite / SQL Server: NULLs sort FIRST in ASC
  • PostgreSQL: NULLS FIRST / NULLS LAST modifier available

Performance Impact

Sorting has a cost

  • Sorting large results is expensive — add LIMIT when possible
  • Index on ORDER BY column avoids a separate sort step
  • ORDER BY + LIMIT = efficient top-N query
  • Avoid ORDER BY in subqueries — it's usually ignored

Power Patterns

Advanced sorting techniques

  • Multi-column: ORDER BY country, name
  • Mixed direction: ORDER BY price DESC, name ASC
  • Expression sort: ORDER BY price * quantity DESC
  • Conditional: ORDER BY CASE WHEN ... END
Syntax
Syntax Template
1SELECT
2 columns
3FROM table
4ORDER BY
5 col1 ASC,
6 col2 DESC;
ASCAscending (default) — smallest/earliest/A first
DESCDescending — largest/latest/Z first
Multiple columnsSecond column breaks ties in the first
Sample Data
products
10 rows
namecategoryprice
Wireless MouseElectronics24.99
Office DeskFurniture189
LED Desk LampFurniture45.5
USB-C CableElectronics12.99
Bluetooth SpeakerElectronics79
Office ChairFurniture95
Coffee MakerAppliances89.99
Notebook SetStationery8.5
Wireless HeadphonesElectronics159
Standing DeskFurniture425
orders
10 rows
idorder_datetotal
12024-03-1524.99
22024-03-16189
32024-03-1845.5
42024-03-20329
52024-03-2212.99
62024-03-25259
72024-03-2895
82024-04-0189.99
92024-04-038.5
102024-04-05425

Worked Example

Show all products sorted by category (A-Z), then by price (highest first) within each category.

SQL
1SELECT
2 name,
3 category,
4 price
5FROM products
6ORDER BY
7 category ASC,
8 price DESC;
First, rows are grouped by category alphabetically (Electronics → Furniture → Stationery). Within each category, products are sorted by price from highest to lowest. Monitor 27inch (329) appears before Mechanical Keyboard (89.99) within Electronics.
Output
10 rows
namecategoryprice
Coffee MakerAppliances89.99
Wireless HeadphonesElectronics159
Bluetooth SpeakerElectronics79
Wireless MouseElectronics24.99
USB-C CableElectronics12.99
Standing DeskFurniture425
Office DeskFurniture189
Office ChairFurniture95
LED Desk LampFurniture45.5
Notebook SetStationery8.5

Key Concepts

1Without ORDER BY, row order is unpredictable — never assume
2You can sort by columns not in your SELECT
3Multi-column ORDER BY uses later columns as tiebreakers
4ASC is the default — you only need to write DESC explicitly

Pro Tip

Unsorted data is hard to read and impossible to paginate. ORDER BY gives results a predictable structure — essential for reports, top-N queries, and leaderboards.

When to Use

Top-selling products, most recent orders, alphabetical customer lists, leaderboards, any ranked listing.

Challenge

Solve the problem below

Show all orders from the orders table sorted by total descending. Display id, order_date, and total. Show only the top 5.

Your Query