`ORDER BY` Clause

Data 360 Query Guide, mirrored from developer.salesforce.com: Use the `ORDER BY` clause to sort query results by one or more columns or expressions.

markdownreferencev1RARJ-R6DFguide: data-cloud-query-guidestage: referencetrust: officialsource: developer.salesforce.comservice: data360interface: sql`ORDER BY` ClauseData 360Data CloudData 360 Query Guide

Created 23 Oct 2026 · Modified 23 Oct 2026 · 1 version

Mirror of <https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/select-order.html> (Salesforce, Data 360 Query Guide), read 2026-10-23. Salesforce's own page is the authority; this copy exists because that site refuses non-browser readers.

ORDER BY Clause

Use the ORDER BY clause to sort query results by one or more columns or expressions.

For each <expression>, use one of these values.

  • Output column name: Sorts by a column in the query result.
  • Output column position: Sorts by the column’s left-to-right position in the result. Use a position when the column doesn’t have a unique name.
  • Expression based on input columns: Sorts by a calculated value. The expression can use a column that isn’t in the SELECT list.

If a name matches both an output column and an input column, ORDER BY uses the output column.

When you sort by multiple expressions, Data 360 SQL evaluates them from left to right. If two rows have the same value for the first expression, the next expression determines their order. Rows that match on every expression are returned in an unspecified order.

You can add sort options after each expression. An option applies only to its preceding expression. For example, ORDER BY x, y DESC sorts x in ascending order and y in descending order. It isn’t the same as ORDER BY x DESC, y DESC.

OptionResult
ASCSorts in ascending order and places null values last. This option is the default.
DESCSorts in descending order and places null values first.
NULLS FIRSTPlaces null values before all non-null values.
NULLS LASTPlaces null values after all non-null values.

Use NULLS FIRST or NULLS LAST to override the default null placement for a sort direction.

Data 360 SQL sorts character strings according to the collation for the column. To use a different collation, add COLLATE to the sort expression. For example, ORDER BY mycolumn COLLATE "en_US".

Use ORDER BY ALL to sort by every column in the query result without listing each column. Data 360 SQL sorts by the first result column, then uses the next column as a tie-breaker. It continues from left to right through all result columns.

Restrictions:

  • You can’t list other sort expressions with ORDER BY ALL.
  • You can’t use ORDER BY ALL with SELECT DISTINCT ON.
  • You can’t use ORDER BY ALL in a window function, aggregate function, or table-valued function. ORDER BY ALL is supported only in a query’s top-level ORDER BY clause.

This query sorts by an input column that isn’t in the SELECT list.

This query sorts first by name and then by code because those columns appear in that order in the SELECT list.

By default, ascending order sorts from lowest to highest and places null values last.

Output:

NULLS FIRST places null values before all non-null values.

Output:

By default, descending order sorts from highest to lowest and places null values first.

Output:

Add NULLS LAST to place null values after all other values when sorting in descending order.

Output:

ORDER BY ALL applies the same sort direction and null placement to every result column. This query uses the default ascending order and places null values last in each column.

Output:

This query sorts every result column in descending order. By default, it places null values first in each column.

Output:

To change where null values appear, add NULLS FIRST or NULLS LAST. This query sorts every column in ascending order and places null values first.

Output:

Read this document as markdown — the same bytes an agent receives.