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

Show:
Database System Support Status Since Version Notes
MySQL ✓ Supported all NULLS LAST via ISNULL()
PostgreSQL ✓ Supported all NULLS FIRST/LAST
SQL Server ✓ Supported all No NULLS FIRST/LAST
Oracle ✓ Supported all NULLS FIRST/LAST
SQLite ✓ Supported all No NULLS FIRST/LAST

Details

Defines row sort order.

Standard Syntax

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

Version Support

MySQL: Since all PostgreSQL: Since all SQL Server: Since all Oracle: Since all SQLite: Since all

Per-Database Syntax & Notes

MySQL

NULLS LAST via ISNULL()

… ORDER BY col IS NULL, col DESC;

PostgreSQL

NULLS FIRST/LAST

… ORDER BY col DESC NULLS LAST;

SQL Server

No NULLS FIRST/LAST

… ORDER BY col DESC;

Oracle

NULLS FIRST/LAST

… ORDER BY col DESC NULLS LAST;

SQLite

No NULLS FIRST/LAST

… ORDER BY col DESC;