ORDER BY determines the sequence in which rows are returned from a SELECT statement. Without it, the database may return rows in any arbitrary order. You can sort by one or many columns, by column aliases, or by expressions. The default direction is ASC (ascending). DESC reverses the order. Most dialects also support NULLS FIRST or NULLS LAST to control where null values appear, a COLLATE clause to apply a specific collation, and positional sorting (ORDER BY 1). Because ORDER BY is executed after SELECT and GROUP BY, you may reference aliases created in the SELECT list. Limiting clauses such as LIMIT, OFFSET, or FETCH FIRST work after ORDER BY, enabling reliable pagination. Sorting requires memory or disk space; large sorts can impact performance unless indexes supporting the requested order exist.
expression
(Any) - Column name, alias, or scalar expression to sort on.ASC
(Keyword) - Sort smallest to largest. Default.DESC
(Keyword) - Sort largest to smallest.NULLS FIRST
(Keyword) - Place nulls before non-nulls.NULLS LAST
(Keyword) - Place nulls after non-nulls.COLLATE
(Keyword) - Apply a specific collation for string comparison.collation_name
(Identifier) - Name of the collation to use.GROUP BY, HAVING, LIMIT, OFFSET, FETCH FIRST, DISTINCT, INDEXES, COLLATE
ANSI SQL-86
ORDER BY sorts the result set returned by a SELECT statement on one or more columns or expressions.
Yes. List each column separated by commas. Ordering is applied left to right.
Use NULLS LAST if the dialect supports it, or sort by an IS NULL expression before the column.
Sorting can be expensive on large datasets. Proper indexing or limiting the result set helps mitigate cost.