ORDER BY with multiple columns
Go Up to Sorting Rows with ORDER BY
If more than one column is specified in an ORDER BY
clause, rows are first arranged by the values in the first column. Then rows that contain the same first-column value are arranged according to the values in the second column, and so on. Each ORDER BY
column can include its own sort order specification.
- Important: In multi-column sorts, after a sort order is specified, it applies to all subsequent columns until another sort order is specified, as in the previous example. This attribute is sometimes called sticky sort order. For example, the following cursor declaration orders retrieval by
LAST_NAME
in descending order, then refines it alphabetically withinLAST_NAME
groups byFIRST_NAME
in ascending order:
EXEC SQL DECLARE PHONE_LIST CURSOR FOR SELECT LAST_NAME, FIRST_NAME, PHONE_EXT FROM EMPLOYEE WHERE PHONE_EXT IS NOT NULL ORDER BY LAST_NAME DESC, FIRST_NAME ASC;