FutureLogic Blog β†’ Databases β†’ IGCSE 0478

How Does SQL ORDER BY Sort Results?
Explained Simply

The SQL ORDER BY clause sorts the rows of a query result using a chosen field. ASC sorts records in ascending order (smallest to largest or A to Z) as the default, while DESC sorts records in descending order (largest to smallest or Z to A).

Sort the Golden Biscuit Tin's BiscuitOrders table by quantity, reverse the order with DESC, and combine WHERE and ORDER BY in one query for Cambridge IGCSE Computer Science (0478 and 0984).

FutureLogic Education thumbnail: SQL ORDER BY – Sort Your Results, with Jacques the pigeon, a chicken and a pirate parrot looking at three cards holding one, two and three biscuits in order

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.

BiscuitOrders table
OrderIDBiscuitTypeQuantity
101Hobnob6
102Viennese Swirl4
103Hobnob8
104Hobnob2
ORDER BY changes the order of the result rows. It does not change which rows are returned.

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;
Output: quantities 2, 4, 6, 8
OrderIDQuantity
1042
1024
1016
1038
  • ORDER BY Quantity sorts the result rows using the Quantity field.
  • ASC means 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;
Output: quantities 8, 6, 4, 2
OrderIDQuantity
1038
1016
1024
1042

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;
  1. WHERE filters the records. Only the Hobnob orders remain: 101 (6), 103 (8) and 104 (2). Order 102 is removed.
  2. ORDER BY sorts what is left. DESC puts the largest quantity first: 8, 6, 2.
  3. SELECT shows the chosen fields: OrderID and Quantity.
Output: order IDs 103, 101, 104
OrderIDQuantity
1038
1016
1042
Order IDs103 Β· 101 Β· 104

3 Hobnob orders, largest first.

The order of the clauses

When a query uses all four clauses, they must be written in this order:

The order of clauses in an SQL queryFour boxes in a row, joined by arrows: SELECT chooses the fields, FROM names the table, WHERE filters the records and ORDER BY sorts the result. WRITE THE CLAUSES IN THIS ORDER SELECTchooses fields β†’ FROMnames the table β†’ WHEREfilters records β†’ ORDER BYsorts the result WHERE is optional. When it is used, it always comes before ORDER BY. ORDER BY goes last, just before the semicolon.
Diagram 1: The four clauses always appear in this order in an IGCSE query.
  • SELECT: which fields to show
  • FROM: which table
  • WHERE: which records to keep (optional)
  • ORDER BY: how to sort the result (optional, always last)
Exam tip: When you trace a query, find the matching records first. Then sort them. Then write only the selected fields. Don't sort the whole table and filter afterwards, because it's easy to lose track of rows that way.

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

βœ— Writing ORDER BY before WHERE

… ORDER BY Quantity DESC WHERE BiscuitType = 'Hobnob'; is in the wrong order. WHERE must come before ORDER BY, and ORDER BY always goes last.

βœ— Sorting the wrong way because the direction was left out

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.

βœ— β€œORDER BY removes rows.”

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

  1. What does ORDER BY change?
  2. What is the default sort direction?
  3. Which keyword sorts largest first?
  4. In a query, which comes first: WHERE or ORDER BY?

Quiz answers

Reveal all answers
  1. The order of the result rows.
  2. Ascending (ASC).
  3. DESC.
  4. 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.

WHERE chooses the rows. ORDER BY lines them up. Hobnobs, largest first: 103, 101, 104.

Ready to total the orders?

You can now choose, filter and sort records. Next, use SUM to add up the values in a field.