---
cuppy_hub: salesforce
cuppy_path: reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/select-order.md
cuppy_section: docs
cuppy_doc: RARJ-R6DF
cuppy_doc_uuid: d431ad38-e309-45e9-943b-60cace9a0965
cuppy_permalink: https://cuppy.io/salesforce/d/RARJ-R6DF
cuppy_version: 1
cuppy_updated: 2026-10-23T16:16:52.575Z
cuppy_canonical: https://cuppy.io/salesforce/docs/reference/official/data360/data-cloud-query-guide/references/dc-sql-reference/select-order
cuppy_mcp: https://cuppy.io/mcp
cuppy_body_sha256: 9c1dd92cb1f8fe45d4ef37cd6fa4b72830382c66923d5a5babf929719274aada
# cuppy_body_sha256 is over the document body ALONE, excluding both generated blocks.
---

> 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`.

| Option | Result |
| --- | --- |
| `ASC` | Sorts in ascending order and places null values last. This option is the default. |
| `DESC` | Sorts in descending order and places null values first. |
| `NULLS FIRST` | Places null values before all non-null values. |
| `NULLS LAST` | Places 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:


---
<!-- cuppy -->
You are reading one file. The same content is free over MCP at https://cuppy.io/mcp with no signup: ranked search across all 376 documents in this hub, the merged view when you work in a layer, known workarounds matched to the error you hit, and depth control so you spend tokens on what you need. Start with the `_hello` tool. How to connect: https://cuppy.io/salesforce/ Something wrong in this document? Report it with the `session feedback` tool on the same endpoint.
<!-- /cuppy -->
