Skip to content
Daniël van Eeden edited this page Mar 13, 2024 · 8 revisions

SQL standard defines the following syntax for ordering:

ORDER BY {expr [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]

This exact syntax is supported by:

Other dialects have some variations:

DB2:

ORDER BY INPUT SEQUENCE

ORDER BY sort_specification [ {"," sort_specification}... ]

sort_specification:
    ORDER OF table_name
  | expr [ASC | DESC] [NULLS FIRST | NULLS LAST]

DB2i:

ORDER [SIBLINGS] BY INPUT SEQUENCE

ORDER [SIBLINGS] BY sort_specification [ {"," sort_specification}... ]

sort_specification:
    ORDER OF table_name
  | expr [ASC | DESC] [NULLS FIRST | NULLS LAST]

Hive and Spark provide ORDER BY, SORT BY, CLUSTER BY, DISTRIBUTE BY:

ORDER BY {column [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]

SORT BY  {column [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]

CLUSTER BY column [ {"," column}... ]

DISTRIBUTE BY column [ {"," column}... ]

MariaDB:

ORDER BY {expr [ASC | DESC]} ["," ...]

MySQL:

ORDER BY {expr [ASC | DESC]} ["," ...] [WITH ROLLUP]

PL/SQL:

ORDER [SIBLINGS] BY {expr [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]

PostgreSQL:

ORDER BY {expr [ASC | DESC | USING operator] [NULLS FIRST | NULLS LAST]} ["," ...]

SingleStoreDB:

ORDER BY {expr [ASC | DESC]} ["," ...]

SQLite:

ORDER BY {expr [COLLATE collation] [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]

TiDB:

ORDER BY {expr [ASC | DESC]} ["," ...]

Transact-SQL:

ORDER BY {expr [COLLATE collation] [ASC | DESC]} ["," ...]
Clone this wiki locally