Sorting with ORDER BY

Without instructions, a database returns rows in whatever order is convenient for it — and that order can change. ORDER BY sorts the result by one or more columns or expressions, ascending or descending.

Sorting by one column

SQL
SELECT name, price
FROM products
ORDER BY price;
Result 12 rows
nameprice
Ceramic Mug12.5
Smart Plug14.99
Jigsaw Puzzle 100018
USB-C Charger19.99
The Patient Gardener24.9
Learning SQL Step by Step32.5
Desk Lamp34
Wooden Train Set39.9
Night Sky Atlas45
Linen Throw49
Wireless Earbuds59.99
Mechanical Keyboard89

The default direction is ascending (ASC): smallest numbers, earliest dates and A–Z text first.

Descending order

SQL
SELECT first_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;
Result 5 rows
first_namesalary
Olivia150000
Mei98000
Jonas85000
Rafael82000
Daniel71000

DESC sorts largest first. (LIMIT, covered next lesson, keeps just the top five.)

Sorting by several columns

SQL
SELECT category, name, price
FROM products
ORDER BY category, price DESC;
Result 12 rows
categorynameprice
BooksNight Sky Atlas45
BooksLearning SQL Step by Step32.5
BooksThe Patient Gardener24.9
ElectronicsMechanical Keyboard89
ElectronicsWireless Earbuds59.99
ElectronicsUSB-C Charger19.99
ElectronicsSmart Plug14.99
HomeLinen Throw49
HomeDesk Lamp34
HomeCeramic Mug12.5
ToysWooden Train Set39.9
ToysJigsaw Puzzle 100018

Rows are sorted by the first column; the second only breaks ties within it. Each column has its own direction — here categories go A–Z, and within each category the most expensive product comes first.

Sorting by expressions and aliases

SQL
SELECT name, ROUND(price * stock, 2) AS stock_value
FROM products
ORDER BY stock_value DESC
LIMIT 4;
Result 4 rows
namestock_value
Wireless Earbuds3599.4
Ceramic Mug2500
USB-C Charger2398.8
Linen Throw1470

Unlike WHERE, ORDER BY runs after the select list is built, so it can use column aliases.

Sorting by column position

SQL
SELECT name, city FROM customers ORDER BY 2, 1;
Result 10 rows
namecity
Farah HaddadNULL
David MüllerBerlin
Ana PereiraLisbon
Julia SantosLisbon
George BrownLondon
Chloé MartinLyon
Elena GarcíaMadrid
Ben CarterManchester
Isaac LeviPorto
Hana NovakPrague

ORDER BY 2 means "the second column in the select list". It's quick for ad-hoc queries but fragile in saved code: change the select list and the sort silently changes.

Where do NULLs go?

SQL
SELECT name, city
FROM customers
ORDER BY city
LIMIT 3;
Result 3 rows
namecity
Farah HaddadNULL
David MüllerBerlin
Ana PereiraLisbon

Databases disagree: SQLite, MySQL and SQL Server put NULL first in ascending order; PostgreSQL and Oracle put it last. The standard NULLS FIRST / NULLS LAST makes it explicit and is supported by SQLite, PostgreSQL and Oracle:

SQL
SELECT name, city
FROM customers
ORDER BY city NULLS LAST;
Result 10 rows
namecity
David MüllerBerlin
Ana PereiraLisbon
Julia SantosLisbon
George BrownLondon
Chloé MartinLyon
Elena GarcíaMadrid
Ben CarterManchester
Isaac LeviPorto
Hana NovakPrague
Farah HaddadNULL

Stable results need a unique sort

SQL
SELECT first_name, salary
FROM employees
WHERE job_title = 'Developer'
ORDER BY salary, id;
Result 2 rows
first_namesalary
Amara68000
Tomás68000

Amara and Tomás earn the same, so sorting by salary alone leaves their order undefined. Adding a unique column such as id as a final tie-breaker guarantees the same order every time — essential for pagination.

Sorting text: collation

How text sorts — uppercase vs lowercase, accented letters, language-specific rules — is decided by the column's collation. SQLite's default compares raw character codes, so 'Z' sorts before 'a'; COLLATE NOCASE ignores case. Other databases offer language-aware collations.

Try it yourself

List orders newest first, and for orders on the same date, by id.

Try it yourself
Result