ORDER BY
Sort the result rows. Without it, the order is not guaranteed.
Syntax
ORDER BY column_name [ASC | DESC];
- Add more columns after commas to break ties.
Example
SELECT name, unit_price FROM products ORDER BY unit_price DESC, name;
Products from the most to the least expensive; equal prices sorted by name.
Tables used: products. Try it in a lesson below.
Common mistakes
- Sorting happens after filtering and grouping, so ORDER BY can use a column alias from SELECT.