-
Notifications
You must be signed in to change notification settings - Fork 407
ORDER BY clause
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}... ]
ORDER BY {expr [ASC | DESC]} ["," ...]
ORDER BY {expr [ASC | DESC]} ["," ...] [WITH ROLLUP]
ORDER [SIBLINGS] BY {expr [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]
ORDER BY {expr [ASC | DESC | USING operator] [NULLS FIRST | NULLS LAST]} ["," ...]
ORDER BY {expr [ASC | DESC]} ["," ...]
ORDER BY {expr [COLLATE collation] [ASC | DESC] [NULLS FIRST | NULLS LAST]} ["," ...]
TiDB:
ORDER BY {expr [ASC | DESC]} ["," ...]
ORDER BY {expr [COLLATE collation] [ASC | DESC]} ["," ...]