ORDER BY Clause - Teradata Database

SQL Data Manipulation Language

Product
Teradata Database
Release Number
15.10
Language
English (United States)
Last Update
2018-10-06
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata® Database

An expression in the SELECT list that specifies how result sets are sorted. If you do not use this clause, result rows are returned unsorted.

where:

 

Syntax Element …

Specifies …

ORDER BY

the order in which result rows are to be sorted.

You can specify one or more sort columns on expression, column_name, column_name_alias, or column_position.

The first sort column you specify determines the column used for the primary (first) sort. Any additional sort columns you specify determines the potential subsequent sorting based on the order in which you specify the columns.

When multiple sort columns are specified, rows are sorted that have the same values of the previous sort column (or columns).

You can specify a sort column to be sorted in either ascending or descending order.

expression

a valid SQL expression on which to sort rows. The expression can specify the numeric position of the expression in the expression list with a name or a constant.

If the sort field is a character string, the expression used in the ORDER BY phrase can include a type modifier to force the sort to be either CASESPECIFIC or NOT CASESPECIFIC.

You can specify scalar subqueries and scalar UDFs, but you cannot reference a column that has a BLOB, CLOB, ARRAY, or VARRAY data type in the ORDER BY expression list.

column_name

the name of a column on which to sort rows. An ordering column does not have to be specified as part of the select expression list.

The maximum number of columns you can specify is 64.

You can specify a column_name_alias instead of column_name (see the description of the column_name_alias element for details). The column_name_alias must not have the same name as a physical column in the table definition. If it does have the same name, you must specify column_position, not column_name_alias. See “Example 4: Ordering on a Column Name Alias” on page 214.

The column you specify cannot have a BLOB, CLOB, ARRAY, or VARRAY data type.

column_name_
alias

a column name alias specified in the select expression list of the query for the column on which the result rows are to be sorted.

If you specify a column_name_alias, that alias cannot match the name of any column that is defined in the table definition for any table referenced in the FROM clause of the query, whether that column is specified in the select list or not. The system references the underlying physical column having the name rather than the column that you attempt to reference using that same name as its alias.

If you attempt to specify such an improper column_name_alias, the system aborts the request and returns an error.

The workaround for this is to specify the sort column by its column_position value within the select list for the query. See “Rules and Restrictions for the ORDER BY Clause” on page 204.

column_position

the numeric position of a column or expression specified in the select expression list on which to sort.

The value you specify must be a positive constant integer literal with a value between 1 and the number of columns specified in the select list, inclusive. Note that Teradata Database treats macro and procedure parameters as expressions, not as the specification of a column position.

If the column_name_alias specified in the select list is the same as the name of the physical column in the underlying table specified in the FROM clause, then you cannot specify that column_name_alias as a sort value. You can specify a column position reference to order on the column or expression in the select list. However, you cannot use a column_name_alias that is the same as the name of some other column specified in the table definition. Substituting column_position for column_name_alias when the query will not otherwise work is not a good practice, and is not recommended. However, it does work if you must resort to it for a given situation.

The column or expression referenced by its numeric position in the select-list must not have either a BLOB or CLOB data type.

This is a Teradata extension to the ANSI SQL:2011 standard.

ASC

Results are to be ordered in ascending sort order.

If the sort field is a character string, the system orders it in ascending order according to the definition of the collation sequence for the current session.

The default order is ASC.

DESC

Results are to be ordered in descending sort order.

If the sort field is a character string, the system orders it in descending order according to the definition of the collation sequence for the current session.

NULLS FIRST

NULL results are to be listed first.

NULLS LAST

NULL results are to be listed last.

The ORDER BY clause is ANSI SQL:2011-compliant with extensions. The specification of an ORDER BY clause in the definition of an updatable cursor is ANSI SQL:2011‑compliant.

The specification of an expression as an ordering argument in the ORDER BY clause is a Teradata extension to the ANSI SQL:2011 standard.

The rules and restrictions for the use of ORDER BY are:

  • Each column_name you specify in an ORDER BY clause must be the name of a column in a relation referenced in the SELECT expression list. The columns named do not have to match the columns in the SELECT expression list.
  • You can also order on a column_name_alias.

    The maximum number of column names you can specify is 64.

    You cannot specify BLOB or CLOB columns in the column name list because you cannot sort on LOB values.

  • You cannot specify case sensitivity for a CLOB column.
  • For details on defining case sensitivity for a column, see SQL Data Types and Literals.

  • The column position you specify in an ORDER BY clause must be an unsigned integer that refers to the sequential position of a column in the SELECT expression list. The column position cannot be passed as a parameter; attempts to do so cause it to be interpreted as an expression, and the data to be sorted by a constant.
  • You cannot specify BLOB or CLOB columns in the column position list because you cannot sort on LOB values.

  • If an ORDER BY specification references a UDT column, then Teradata Database uses the defined ordering functionality for that UDT to order the result set.
  • You can specify a scalar subquery as a column expression in the ORDER BY clause of a query.
  • You can specify an ORDER BY clause before or after any WITH clause. Also see “ORDER BY Clause” on page 202.
  • When you specify both WITH and ORDER BY clauses in the same SELECT request, they function together as follows:

  • The WITH clause defines the major sort key.
  • The ORDER BY clause defines the minor sort key.
  • This is true regardless of the structure of the query or the number of WITH clauses. See “Example 3: Combining WITH and ORDER BY Clauses” on page 223.

  • If you have multiple ON clauses, you cannot specify ORDER BY as the only option in an ON clause. You must combine it with a PARTITION BY (ANY), HASH BY, or DIMENSION clause.
  • If you specify an ORDER BY or LOCAL ORDER BY clause with PARTITION BY or HASH BY input, then the following is required:
  • All inputs must have the same number of ORDER BY columns.
  • The data types of the columns must be the same type or matched using implicit cast.
  • This section describes some unexpected results that can occur when using the ORDER BY clause. They are:

  • Unexpected row length errors.
  • Unexpected sort order when querying DATE data type columns. See “Unexpected Sort Order When Querying DATE Data Type Columns” on page 206.