ORDER BY Single and Multiple Columns
Sort results ascending or descending, sort by multiple columns, and control where NULLs land with NULLS FIRST / NULLS LAST.
Why You Need ORDER BY
A SQL table is an unordered set. Without ORDER BY, the database may return rows in any order — and that order can change between runs. If order matters in your UI or report, sort explicitly.
Sorting by One Column
Default sort is ascending:
SELECT id, email FROM users ORDER BY created_at; -- oldest first
SELECT id, email FROM users ORDER BY created_at ASC;
SELECT id, email FROM users ORDER BY created_at DESC; -- newest firstAll lessons in this course
- Selecting Columns and Expressions
- WHERE Filters: Comparison and Logic Operators
- ORDER BY Single and Multiple Columns
- LIMIT, OFFSET and Pagination Basics