Sorting with ORDER BY
Handout
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Sorting the result
The rows come back in no particular order. Add ORDER BY to sort them by a column:
SELECT name, score FROM student ORDER BY score DESC;
ASCsorts ascending (smallest first) — this is the default.DESCsorts descending (largest first).
Sorting by more than one column
Sort text columns alphabetically:
SELECT name FROM student ORDER BY name;
You can sort by several columns: ORDER BY form ASC, score DESC sorts by form first, then by score within each form. ORDER BY always comes last in the query.
Show every student's name and score, highest score first.
Click Run to see the output here.
Show every student's name, sorted alphabetically (A to Z).
Click Run to see the output here.