Guided Notes

ORDER BY & Dates

Sorting results, LIMIT, working with dates and timestamps, and extracting parts of a date.

Sorting with ORDER BY

Slide 1 / 5

ORDER BY sorts the final result. Ascending (A→Z, smallest first) is the default; add DESC for descending:

SELECT state, num_accidents FROM accident_counts ORDER BY num_accidents DESC;

Sort by several columns by listing them β€” ORDER BY state, num_accidents DESC sorts by state, then by accidents within each state.

Without ORDER BY, a database may return rows in any order at all.