
Which order was the smallest?
Without ORDER BY, SQL doesn't promise any particular row order (see SQL SELECT and FROM Explained). Jacques wants to list the orders from smallest to largest so he can pack the small ones first. He adds an ORDER BY clause.
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
Cambridge syllabus focus
Topic 9.5: understand and complete SQL scripts that use ORDER BY to sort the results of a query in ascending or descending order.
Worked example 1: ascending order
SELECT OrderID, Quantity FROM BiscuitOrders ORDER BY Quantity ASC;
| OrderID | Quantity |
|---|---|
| 104 | 2 |
| 102 | 4 |
| 101 | 6 |
| 103 | 8 |
ORDER BY Quantitysorts the result rows using the Quantity field.ASCmeans ascending: smallest to largest for numbers, A to Z for text.- Every row stays together. Order 104 still has quantity 2, so each record moves as a whole.
Ascending is the default
If you leave ASC out, SQL sorts in ascending order anyway. These two clauses give exactly the same result:
ORDER BY Quantity ASC
Ascending, written out in full.
ORDER BY Quantity
Also ascending, because ASC is the default.
Worked example 2: descending order with DESC
SELECT OrderID, Quantity FROM BiscuitOrders ORDER BY Quantity DESC;
| OrderID | Quantity |
|---|---|
| 103 | 8 |
| 101 | 6 |
| 102 | 4 |
| 104 | 2 |
DESC means descending: largest to smallest for numbers, Z to A for text. It reverses the ascending order exactly.
Check your understanding 1
In what order would SELECT OrderID FROM BiscuitOrders ORDER BY Quantity; list the OrderIDs?
Reveal answer
104, 102, 101, 103. The rows are sorted by Quantity in ascending order (the default), even though Quantity itself is not displayed.
Worked example 3: WHERE and ORDER BY together
Now Jacques wants only the Hobnob orders, largest first:
SELECT OrderID, Quantity FROM BiscuitOrders WHERE BiscuitType = 'Hobnob' ORDER BY Quantity DESC;
- WHERE filters the records. Only the Hobnob orders remain: 101 (6), 103 (8) and 104 (2). Order 102 is removed.
- ORDER BY sorts what is left. DESC puts the largest quantity first: 8, 6, 2.
- SELECT shows the chosen fields: OrderID and Quantity.
| OrderID | Quantity |
|---|---|
| 103 | 8 |
| 101 | 6 |
| 104 | 2 |
3 Hobnob orders, largest first.
The order of the clauses
When a query uses all four clauses, they must be written in this order:
SELECT: which fields to showFROM: which tableWHERE: which records to keep (optional)ORDER BY: how to sort the result (optional, always last)
Check your understanding 2
Does running ORDER BY Quantity DESC change the order of the records stored in the BiscuitOrders table?
Reveal answer
No. ORDER BY only sorts the rows in this query's result. The stored table is unchanged.
Common Cambridge exam mistakes
β¦ ORDER BY Quantity DESC WHERE BiscuitType = 'Hobnob'; is in the wrong order. WHERE must come before ORDER BY, and ORDER BY always goes last.
If the question asks for the largest first, you must write DESC. Leaving it out gives ascending order (smallest first), because ASC is the default.
Sorting never removes or adds records. The query above returns 3 rows because WHERE kept 3, not because of ORDER BY.
Original FutureLogic exam-style practice
These are original teaching questions, not official Cambridge examination questions. The marks shown are a guide only.
Question 1 (3 marks)
Write an SQL query to display the BiscuitType and Quantity of every order, with the largest quantity first.
Reveal model answer
SELECT BiscuitType, Quantity FROM BiscuitOrders ORDER BY Quantity DESC;
1 mark for SELECT BiscuitType, Quantity, 1 mark for FROM BiscuitOrders and 1 mark for ORDER BY Quantity DESC.
Question 2 (2 marks)
State the output of this query, in the correct order.
SELECT OrderID FROM BiscuitOrders WHERE Quantity > 3 ORDER BY Quantity;
Reveal model answer
The records with a quantity greater than 3 are 101 (6), 102 (4) and 103 (8) (1). Sorted ascending by Quantity, the output is 102, 101, 103 (1).
Quick quiz
- What does ORDER BY change?
- What is the default sort direction?
- Which keyword sorts largest first?
- In a query, which comes first: WHERE or ORDER BY?
Quiz answers
Reveal all answers
- The order of the result rows.
- Ascending (ASC).
- DESC.
- WHERE.
Final summary
ORDER BY
Sorts the result rows by a chosen field.
ASC
Smallest first (2, 4, 6, 8). This is the default.
DESC
Largest first (8, 6, 4, 2).
Clause order
SELECT β FROM β WHERE β ORDER BY.