ORDER BY

The clause that sorts a query's answer by one or more columns, each ascending or descending.

In more detail

A table has no order of its own, so without ORDER BY any order is a correct answer and may change when data or indexes change. With several sort keys, later keys break ties in earlier ones. Sorting happens last, after filtering and grouping.

Examples

ORDER BY team, salary DESC
In this course: Without ORDER BY a database may return rows in any order and change that order between runs, so a test must never rely on row order it did not ask for. Asserting on an unordered result is a common cause of flaky database tests.

Related terms

Taught in

References

Certification courses