Sorting with ORDER BY
Without instructions, a database returns rows in whatever order is quickest for it. That order can
change from one run to the next. When order matters, say so with ORDER BY:
SELECT name, list_price
FROM products
ORDER BY list_priceORDER BY goes at the end of the query, after WHERE. It sorts from lowest to highest, earliest
to latest, or A to Z. Add DESC (descending) to reverse it:
ORDER BY list_price DESCYou can write ASC (ascending) for the normal direction, but it's the default, so it's optional.
Sorting by more than one column
List several columns to break ties. This sorts suppliers by country, then alphabetically within each country:
SELECT name, country
FROM suppliers
ORDER BY country, nameEach column can have its own direction: ORDER BY country, budget DESC.