SQL ORDER BY: sort the results
Without specific instructions, a database returns rows in whatever order suits it, and that order can change from one run to the next. To get a reliable ranking (cheapest to most expensive, newest to oldest, alphabetical order), you add ORDER BY.
Syntax
SELECT columns
FROM table
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];How it works
ASCsorts in ascending order (A → Z, 1 → 9, oldest → newest). It is the default, so you can leave it out.DESCsorts in descending order.- You can sort on several columns: the second one is only used to break ties on the first.
ORDER BY goes at the end of the query, after WHERE, GROUP BY and HAVING. Since it runs last (just before LIMIT), it can use the aliases defined in the SELECT.
In SQLite, NULL values are considered the smallest: they come first in ascending order. You can change this with NULLS LAST.
Diagram
| nom | prix |
|---|---|
| Plaid | 25 |
| Clavier | 49 |
| Gourde | 12 |
| nom | prix |
|---|---|
| Clavier | 49 |
| Plaid | 25 |
| Gourde | 12 |
Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).
Glossary: the sample database uses French names
clients- customers
commandes- orders
lignes_commande- order lines
produits- products
fournisseurs- suppliers
employes- employees
prenom / nom- first name / last name (or name)
ville- city
pays- country
prix- price
prix_unitaire- unit price
quantite- quantity
categorie- category
date_commande- order date
poste- job title
departement- department
salaire- salary
statut- status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled
Examples with their results
1. Ascending order
The products from cheapest to most expensive.
SELECT nom, prix
FROM produits
ORDER BY prix
LIMIT 8; Try it in the sandbox| nom | prix |
|---|---|
| Miel bio | 3.7 |
| Tapis de yoga | 6.53 |
| Roman policier | 8.03 |
| BD aventure | 9.98 |
| Coussin | 10.86 |
| Ballon de foot | 14.68 |
| Clavier mécanique | 15 |
| Vase céramique | 18.35 |
8 rows in total
2. Descending order
The most recent orders first.
SELECT id, date_commande, statut
FROM commandes
ORDER BY date_commande DESC
LIMIT 6; Try it in the sandbox| id | date_commande | statut |
|---|---|---|
| 473 | 2025-12-28 | livrée |
| 294 | 2025-12-22 | livrée |
| 224 | 2025-12-20 | livrée |
| 371 | 2025-12-20 | expédiée |
| 417 | 2025-12-18 | en cours |
| 509 | 2025-12-18 | livrée |
6 rows in total
3. Sort on several columns
By department (A → Z), then by descending salary within each department.
SELECT departement, prenom, nom, salaire
FROM employes
ORDER BY departement, salaire DESC
LIMIT 10; Try it in the sandbox| departement | prenom | nom | salaire |
|---|---|---|---|
| Direction | Chloé | Dubois | 9,500 |
| IT | Nathan | Roux | 6,800 |
| IT | Clara | Garcia | 5,100 |
| IT | Inès | Moreau | 4,500 |
| IT | Chloé | Blanc | 3,800 |
| IT | Inès | Faure | 3,600 |
| RH | Louis | Chevalier | 5,300 |
| RH | Chloé | Benali | 4,900 |
| RH | Samir | Simon | 4,800 |
| RH | Chloé | Roux | 3,600 |
10 rows in total
4. Sort on a calculation
You can sort on an alias defined in the SELECT.
SELECT nom, prix * stock AS valeur_stock
FROM produits
ORDER BY valeur_stock DESC
LIMIT 5; Try it in the sandbox| nom | valeur_stock |
|---|---|
| Écran 27" | 34,884 |
| SSD 1To | 28,530 |
| Bougie parfumée | 9,564.44 |
| Hub USB-C | 7,938 |
| Souris sans fil | 7,725 |
5 rows in total
Common mistakes
Thinking DESC applies to every column
DESC only applies to the column written right before it. Here, departement stays in ascending order.
SELECT * FROM employes ORDER BY salaire, departement DESC;SELECT * FROM employes ORDER BY salaire DESC, departement DESC;Putting ORDER BY before WHERE
The clause order is fixed: WHERE always comes before ORDER BY.
SELECT * FROM produits ORDER BY prix WHERE stock > 0;SELECT * FROM produits WHERE stock > 0 ORDER BY prix;Relying on the natural order of the table
Without ORDER BY, the row order is not guaranteed, even if it looks stable. Always add ORDER BY when the order matters.
SELECT nom FROM produits LIMIT 3;SELECT nom FROM produits ORDER BY nom LIMIT 3;Exercises
Try writing the query yourself before opening the solution.
Challenge 1. Show the customers sorted by city (A → Z), then by last name.
Show a solution
SELECT ville, nom, prenom FROM clients ORDER BY ville, nom;| ville | nom | prenom |
|---|---|---|
| Bordeaux | Benali | Lucas |
| Bordeaux | Bernard | Adam |
| Bordeaux | Blanc | Lucas |
| Bordeaux | Bonnet | Rayan |
| Bordeaux | Durand | Omar |
| Bordeaux | Faure | Zoé |
| Bordeaux | Garcia | Camille |
| Bordeaux | Garcia | Camille |
120 rows in total · 8 shown
Try it in the sandboxChallenge 2. Show the products from the largest stock to the smallest.
Show a solution
SELECT nom, stock FROM produits ORDER BY stock DESC;| nom | stock |
|---|---|
| Gourde inox | 199 |
| Livre de cuisine | 183 |
| Café en grains | 173 |
| Hub USB-C | 162 |
| Raquette | 162 |
| Bougie parfumée | 157 |
| Vase céramique | 128 |
| Corde à sauter | 109 |
29 rows in total · 8 shown
Try it in the sandboxThe interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.
Frequently asked questions
What is the default order of ORDER BY?
Ascending (ASC). ORDER BY prix and ORDER BY prix ASC are identical.
Can I sort on a column that is not displayed?
Yes, in a simple query. For example SELECT nom FROM produits ORDER BY prix sorts by price without showing it.
Can I write ORDER BY 2?
Yes, the number is the position of the column in the SELECT. It is handy but fragile: if you change the column order, the sort changes too.