0984

Database Concepts

Databases · 5 question types

ORDER BY sorts the records in the result by a chosen field, either ascending or descending.

ORDER BY is added after the WHERE clause (or after FROM if there is no WHERE). The direction is set with ASC (ascending: A→Z, smallest first) or DESC (descending: Z→A, largest first). If neither is written, the default is ascending.

SELECT Species, DailyFeedKg
FROM tbl_zoo
WHERE InCaptivity = 'Yes'
ORDER BY DailyFeedKg DESC;

This lists the captive species from the largest daily feed to the smallest, so Giraffe (35) appears first, then Bison (25), then Gorilla (20), and so on down to Macaw (1).

You can sort on text too. ORDER BY Species ASC would list the species in alphabetical order.