What if the correct records are in the wrong order?
SELECT chooses the fields. FROM chooses the table. WHERE may filter the records.
ORDER BY then sorts the records in the output using one chosen field.
In Cambridge IGCSE Computer Science (0478 and 0984), the SQL ORDER BY clause sorts query output rows using a specified field. Records are sorted in ascending order (ASC, lowest to highest or A to Z) by default unless DESC is written for descending order.
Practise sorting numbers and text in the Students table so you always apply the right sort direction in Topic 9.5 exam questions.
SELECT chooses the fields. FROM chooses the table. WHERE may filter the records.
ORDER BY then sorts the records in the output using one chosen field.
SELECT Name, Score FROM Students ORDER BY Score;
The learning map stays the same. ORDER BY only changes the order of the result.
| StudentID | Name | Age | Score |
|---|---|---|---|
| 101 | Alice | 15 | 81 |
| 102 | Ben | 16 | 65 |
| 103 | Chen | 15 | 90 |
| 104 | Dia | 16 | 72 |
SELECT Name, Score FROM Students ORDER BY Score;
| Name | Score |
|---|---|
| Ben | 65 |
| Dia | 72 |
| Alice | 81 |
| Chen | 90 |
Lowest to highest. This is the default.
Highest to lowest.
SELECT Name FROM Students ORDER BY Name;
Choose the Name field from Students and sort the names alphabetically.
This is descending order.
Ascending order is the default.