SQL Quest
Sign in Français 0 XP Lvl 1
Beginner

SQL ORDER BY: sort the results

4 min read · 4 runnable examples · updated October 9, 2026

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

  • ASC sorts in ascending order (A → Z, 1 → 9, oldest → newest). It is the default, so you can leave it out.
  • DESC sorts 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

produits
nomprix
Plaid25
Clavier49
Gourde12
result
nomprix
Clavier49
Plaid25
Gourde12
ORDER BY prix DESC: the rows are reordered from the highest price to the lowest.

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
Result
nomprix
Miel bio3.7
Tapis de yoga6.53
Roman policier8.03
BD aventure9.98
Coussin10.86
Ballon de foot14.68
Clavier mécanique15
Vase céramique18.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
Result
iddate_commandestatut
4732025-12-28livrée
2942025-12-22livrée
2242025-12-20livrée
3712025-12-20expédiée
4172025-12-18en cours
5092025-12-18livré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
Result
departementprenomnomsalaire
DirectionChloéDubois9,500
ITNathanRoux6,800
ITClaraGarcia5,100
ITInèsMoreau4,500
ITChloéBlanc3,800
ITInèsFaure3,600
RHLouisChevalier5,300
RHChloéBenali4,900
RHSamirSimon4,800
RHChloéRoux3,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
Result
nomvaleur_stock
Écran 27"34,884
SSD 1To28,530
Bougie parfumée9,564.44
Hub USB-C7,938
Souris sans fil7,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.

Avoid
SELECT * FROM employes ORDER BY salaire, departement DESC;
Correct
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.

Avoid
SELECT * FROM produits ORDER BY prix WHERE stock > 0;
Correct
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.

Avoid
SELECT nom FROM produits LIMIT 3;
Correct
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;
villenomprenom
BordeauxBenaliLucas
BordeauxBernardAdam
BordeauxBlancLucas
BordeauxBonnetRayan
BordeauxDurandOmar
BordeauxFaureZoé
BordeauxGarciaCamille
BordeauxGarciaCamille

120 rows in total · 8 shown

Try it in the sandbox

Challenge 2. Show the products from the largest stock to the smallest.

Show a solution
SELECT nom, stock FROM produits ORDER BY stock DESC;
nomstock
Gourde inox199
Livre de cuisine183
Café en grains173
Hub USB-C162
Raquette162
Bougie parfumée157
Vase céramique128
Corde à sauter109

29 rows in total · 8 shown

Try it in the sandbox
Want more practice?

The interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.

Do the interactive exercises

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.

Read next