ORDER BY
ASC
is optional because it's default value
ORDER BY
clause in
SELECT
statement
SELECT first_name, last_name 
FROM students
ORDER BY id DESC
Orders the result by
last_name
with
ascending
order
SELECT first_name, last_name 
FROM students
ORDER BY last_name ASC
ORDER BY
with several fields
SELECT first_name, last_name 
FROM students
ORDER BY last_name ASC, first_name ASC
Grouped by
last_name
and Ordered by
Average points
SELECT 
    last_name,
    AVG(points) average_points
FROM students
GROUP BY last_name
ORDER BY average_points DESC
Mathematical expression used to order the result
SELECT *
FROM `orders` 
ORDER BY price * quantity DESC
Order the result using
alias
SELECT *, price * quantity AS total
FROM `orders` 
ORDER BY total DESC
Orders the result by first name with
ASC
and by id with
DESC
using column numbers
SELECT
    id,
    first_name
FROM `students`
ORDER BY 2, 1 DESC
by Valeri Tandilashvili
4 years ago
MySQL
ORDER BY
1
Pro tip: use ```triple backticks around text``` to write in code fences