Sort the result set by one or more columns.

Sorts the result set by one or more columns in ascending (default) or descending order. Always evaluated last in the query pipeline, after WHERE, GROUP BY, and HAVING. Sorting large result sets is expensive; push sorting to application logic when possible.

Compatibility

Filter by Database
SQL ORDER BY Compatibility Across Databases
Database System Support Status Since Version Notes
MySQL Native all NULLS LAST via ISNULL()
PostgreSQL Native all NULLS FIRST/LAST
SQL Server Native all No NULLS FIRST/LAST
Oracle Native all NULLS FIRST/LAST
SQLite Native all No NULLS FIRST/LAST

Details

Defines row sort order.

Standard Syntax

SELECT * FROM tbl ORDER BY col1 DESC, col2 ASC;

Version Support

MySQL: Native in all listed versions PostgreSQL: Native in all listed versions SQL Server: Native in all listed versions Oracle: Native in all listed versions SQLite: Native in all listed versions

Per-Database Syntax & Notes

MySQL Native syntax

NULLS LAST via ISNULL()

… ORDER BY col IS NULL, col DESC;

PostgreSQL Native syntax

NULLS FIRST/LAST

… ORDER BY col DESC NULLS LAST;

SQL Server Native syntax

No NULLS FIRST/LAST

… ORDER BY col DESC;

Oracle Native syntax

NULLS FIRST/LAST

… ORDER BY col DESC NULLS LAST;

SQLite Native syntax

No NULLS FIRST/LAST

… ORDER BY col DESC;