6.1.9. ORDER BY

When a SELECT statement is executed, the result set is not sorted in any way. It often happens that rows appear to be sorted chronologically, simply because they are returned in the same order they were added to the table by INSERT statements. To specify a sorting order for the set specification, an ORDER BY clause is used.

Syntax

  1. SELECT ... FROM ...
  2. ...
  3. ORDER BY <ordering-item> [, <ordering-item> …]
  4. <ordering-item> ::=
  5. {col-name | col-alias | col-position | <expression>}
  6. [COLLATE collation-name]
  7. [ASC[ENDING] | DESC[ENDING]]
  8. [NULLS {FIRST|LAST}]
Table 68. Arguments for the ORDER BY Clause
ArgumentDescription

col-name

Full column name

col-alias

Column alias

col-position

Column position in the SELECT list

expression

Any expression

collation-name

Collation name (sorting order for string types)

Description

The ORDER BY consists of a comma-separated list of the columns on which the result data set should be sorted. The sort order can be specified by the name of the column — but only if the column was not previously aliased in the SELECT columns list. The alias must be used if it was used there. The ordinal position number of the column in the SELECT column list, the alias given to the column in the SELECT list with the help of the AS keyword, or the number of the column in the SELECT list can be used without restriction.

The three forms of expressing the columns for the sort order can be mixed in the same ORDER BY clause. For instance, one column in the list can be specified by its name and another column can be specified by its number.

If you use the column position to specify the sort order for a query of the SELECT * style, the server expands the asterisk to the full column list in order to determine the columns for the sort. It is, however, considered “sloppy practice” to design ordered sets this way.

Sorting Direction

The keyword ASCENDING, usually abbreviated to ASC, specifies a sort direction from lowest to highest. ASCENDING is the default sort direction.

The keyword DESCENDING, usually abbreviated to DESC, specifies a sort direction from highest to lowest.

Specifying ascending order for one column and the descending order for another is allowed.

Collation Order

The keyword COLLATE specifies the collation order for a string column if you need a collation that is different from the normal one for this column. The normal collation order will be either the default one for the database character set or one that has been set explicitly in the column’s definition.

NULLs Position

The keyword NULLS defines where NULL in the associated column will fall in the sort order: NULLS FIRST places the rows with the NULL column above rows ordered by that column’s value; NULLS LAST places those rows after the ordered rows.

NULLS FIRST is the default.

Ordering UNION-ed Sets

The discrete queries contributing to a UNION cannot take an ORDER BY clause. The only option is to order the entire output, using one ORDER BY clause at the end of the overall query.

The simplest — and, in some cases, the only — method for specifying the sort order is by the ordinal column position. However, it is also valid to use the column names or aliases, from the first contributing query only.

The ASC/DESC and/or NULLS directives are available for this global set.

If discrete ordering within the contributing set is required, use of derived tables or common table expressions for those sets may be a solution.

Examples of ORDER BY

Sorting the result set in ascending order, ordering by the RDB$CHARACTER_SET_ID, RDB$COLLATION_ID columns of the RDB$COLLATIONS table:

  1. SELECT
  2. RDB$CHARACTER_SET_ID AS CHARSET_ID,
  3. RDB$COLLATION_ID AS COLL_ID,
  4. RDB$COLLATION_NAME AS NAME
  5. FROM RDB$COLLATIONS
  6. ORDER BY RDB$CHARACTER_SET_ID, RDB$COLLATION_ID;

The same, but sorting by the column aliases:

  1. SELECT
  2. RDB$CHARACTER_SET_ID AS CHARSET_ID,
  3. RDB$COLLATION_ID AS COLL_ID,
  4. RDB$COLLATION_NAME AS NAME
  5. FROM RDB$COLLATIONS
  6. ORDER BY CHARSET_ID, COLL_ID;

Sorting the output data by the column position numbers:

  1. SELECT
  2. RDB$CHARACTER_SET_ID AS CHARSET_ID,
  3. RDB$COLLATION_ID AS COLL_ID,
  4. RDB$COLLATION_NAME AS NAME
  5. FROM RDB$COLLATIONS
  6. ORDER BY 1, 2;

Sorting a SELECT * query by position numbers — possible, but nasty and not recommended:

  1. SELECT *
  2. FROM RDB$COLLATIONS
  3. ORDER BY 3, 2;

Sorting by the second column in the BOOKS table:

  1. SELECT
  2. BOOKS.*,
  3. FILMS.DIRECTOR
  4. FROM BOOKS, FILMS
  5. ORDER BY 2;

Sorting in descending order by the values of column PROCESS_TIME, with NULLs placed at the beginning of the set:

  1. SELECT *
  2. FROM MSG
  3. ORDER BY PROCESS_TIME DESC NULLS FIRST;

Sorting the set obtained by a UNION of two queries. Results are sorted in descending order for the values in the second column, with NULLs at the end of the set; and in ascending order for the values of the first column with NULLs at the beginning.

  1. SELECT
  2. DOC_NUMBER, DOC_DATE
  3. FROM PAYORDER
  4. UNION ALL
  5. SELECT
  6. DOC_NUMBER, DOC_DATE
  7. FROM BUDGORDER
  8. ORDER BY 2 DESC NULLS LAST, 1 ASC NULLS FIRST;