---
schema_version: "1"
id: 75ebd99c7bb9fba67c7dccb07de7266aabb28fbd19a6f44c97d22617c7b36da5
title: ORDER BY Clause
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_sql/guides/bi-sql-orderby.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-sql/guide/bi-sql-orderby.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 938fc6bb17e606b127b4c700c1974c68acd2e05351c83d89bdb530b685585327
products:
  - SQL for Analytics Developer Guide
  - analytics
---

# ORDER BY Clause

`SELECT` returns rows in an unspecified order by default. To sort returned rows in ascending or descending order, use the `ORDER BY` clause.

`ORDER BY` takes the following syntax.

```nolang
ORDER BY field_name(s) [ ASC | DESC ] [ NULLS { FIRST | LAST }]
```

This example shows how `ORDER BY` works with multiple fields.

```nolang
SELECT State, Category, Sub_Category FROM "Superstore" ORDER BY State, Category;
```

Here, the `SELECT` statement orders by the State and Category columns. Alabama and Furniture are the first alphabetized fields for their respective columns. For all of the rows where State is equal to Alabama and Category is equal to Furniture, SQL returns the Sub-Category column values without any order. When State is equal to Alabama, and Category has displayed all of the Furniture values, the Category column displays Office Supplies, the next value in alphabetical order.

| State   | Category        | Sub-Category |
| ------- | --------------- | ------------ |
| Alabama | Furniture       | Chairs       |
| Alabama | Furniture       | Chairs       |
| Alabama | Furniture       | Chairs       |
| Alabama | Furniture       | Chairs       |
| Alabama | Furniture       | Furnishings  |
| Alabama | Furniture       | Tables       |
| Alabama | Furniture       | Chairs       |
| Alabama | Furniture       | Furnishings  |
| Alabama | Furniture       | Tables       |
| Alabama | Furniture       | Furnishings  |
| Alabama | Furniture       | Tables       |
| Alabama | Furniture       | Chairs       |
| Alabama | Office Supplies | Appliances   |

To order the columns in ascending or descending order, use the keywords `ASC` and `DSC`. The order is ascending by default. To sort fields that contain null values, specify `NULLS FIRST` or `NULLS LAST`. By default, null values are treated as the largest. In ascending order, they are ordered last. In descending order, they are first.
