---
title: ORDER BY
description: Sort ShopifyQL query results by one or more columns.
api_version: 2026-07
source_url:
  html: 'https://shopify.dev/docs/api/shopifyql/latest/syntax/order-by'
  md: 'https://shopify.dev/docs/api/shopifyql/latest/syntax/order-by.md'
api_name: shopifyql
---

# ORDER BY

Use `ORDER BY` to sort results by one or more columns, each ascending or descending. Set the [sort direction](#sort-direction) for each column, and rely on the query's [ordering behavior](#ordering-behavior) when you combine `ORDER BY` with [`TIMESERIES`](https://shopify.dev/docs/api/shopifyql/latest/syntax/timeseries). You can sort by columns from [`SHOW`](https://shopify.dev/docs/api/shopifyql/latest/syntax/from-and-show#show) or by [generated columns](#sorting-generated-columns) that clauses like [`WITH`](https://shopify.dev/docs/api/shopifyql/latest/syntax/with) and [`COMPARE TO`](https://shopify.dev/docs/api/shopifyql/latest/syntax/compare-to) add.

## Syntax

```shopifyql
ORDER BY column [ASC | DESC][, ...]
```

***

## Sort direction

`ORDER BY` sorts each column ascending by default. Add `ASC` to sort ascending or `DESC` to sort descending. When you list more than one column, ShopifyQL sorts by each in turn, using later columns to order rows that are equal on the earlier ones.

Examples

### Examples

* ####

  ##### Description

  Sort monthly \[\`new\_customer\_records\`]\(/docs/api/shopifyql/latest/schemas/customers/customers#customersmetric-propertydetail-newcustomerrecords) for the year to date. This example uses \`ORDER BY month ASC\` to sort the months from oldest to newest.

  ##### ShopifyQL

  ```shopifyql
  FROM customers
    SHOW new_customer_records
    GROUP BY month
    SINCE startOfYear(0y) UNTIL today
    ORDER BY month ASC
  ```

* ####

  ##### Description

  Rank last month's \[\`net\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-netsales) by product within each sales channel. This example uses \`ORDER BY sales\_channel ASC, net\_sales DESC\` to sort by channel first, then by \`net\_sales\` within each channel.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW net_sales
    GROUP BY sales_channel, product_title
    DURING last_month
    ORDER BY sales_channel ASC, net_sales DESC
    LIMIT 25
  ```

***

## Ordering behavior

The order a query returns depends on whether it uses [`TIMESERIES`](https://shopify.dev/docs/api/shopifyql/latest/syntax/timeseries), `ORDER BY`, or both:

* When a query uses `TIMESERIES` without `ORDER BY`, results are ordered by the time dimension.
* When a query uses `TIMESERIES` with `ORDER BY`, the time dimension stays the primary order, then `ORDER BY` columns break ties.
* When a query uses `ORDER BY` without `TIMESERIES`, rows are sorted by the specified dimensions or metrics.
* When a query has neither `TIMESERIES` nor `ORDER BY`, results are ordered by the first `SHOW` column.

Examples

### Examples

* ####

  ##### Description

  Break out last week's daily \[\`net\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-netsales) by sales channel. This example adds \`ORDER BY net\_sales DESC\` to a \`TIMESERIES\` query, so results order by day first and then rank channels by revenue within each day.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW net_sales
    GROUP BY sales_channel
    TIMESERIES day
    DURING last_week
    ORDER BY net_sales DESC
  ```

* ####

  ##### Description

  Rank last month's \[\`net\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-netsales) by sales channel. This example uses \`ORDER BY net\_sales DESC\` without \`TIMESERIES\`, so ShopifyQL sorts all grouped rows directly by \`net\_sales\`.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW net_sales
    GROUP BY sales_channel
    DURING last_month
    ORDER BY net_sales DESC
  ```

* ####

  ##### Description

  Group last month's \[\`sessions\`]\(/docs/api/shopifyql/latest/schemas/sessions\_and\_behavior/sessions#sessionsmetric-propertydetail-sessions) by device type with no \`ORDER BY\`. This example shows how results fall back to the default order of the first \`SHOW\` column when no sort is given.

  ##### ShopifyQL

  ```shopifyql
  FROM sessions
    SHOW sessions
    GROUP BY session_device_type
    DURING last_month
  ```

* ####

  ##### Description

  List last week's daily \[\`sessions\`]\(/docs/api/shopifyql/latest/schemas/sessions\_and\_behavior/sessions#sessionsmetric-propertydetail-sessions) with no \`ORDER BY\`. This example shows how a \`TIMESERIES\` query orders by its time dimension automatically when no sort is given.

  ##### ShopifyQL

  ```shopifyql
  FROM sessions
    SHOW sessions
    TIMESERIES day
    DURING last_week
  ```

***

## Sorting generated columns

`ORDER BY` can sort the columns that other clauses generate, such as comparison columns from [`COMPARE TO`](https://shopify.dev/docs/api/shopifyql/latest/syntax/compare-to) and totals or cumulative columns from [`WITH`](https://shopify.dev/docs/api/shopifyql/latest/syntax/with). Reference a generated column by the name that the generating clause defines.

Examples

### Examples

* ####

  ##### Description

  Rank product types by \[\`gross\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-grosssales) for the current year. This example uses the generated \`comparison\_gross\_sales\_\_previous\_year\` column in \`ORDER BY\` to sort by the previous year.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW gross_sales
    GROUP BY product_type
    DURING this_year
    COMPARE TO previous_year
    ORDER BY comparison_gross_sales__previous_year DESC
    LIMIT 10
  ```

* ####

  ##### Description

  Rank sales-channel and product-type rows by \[\`net\_sales\`]\(/docs/api/shopifyql/latest/schemas/sales\_revenue/sales#salesmetric-propertydetail-netsales) for last month. This example uses the generated \`net\_sales\_\_sales\_channel\_totals\` column in \`ORDER BY\` to sort by each channel's subtotal before ranking product types within that channel.

  ##### ShopifyQL

  ```shopifyql
  FROM sales
    SHOW net_sales
    GROUP BY sales_channel, product_type WITH GROUP_TOTALS
    DURING last_month
    ORDER BY net_sales__sales_channel_totals DESC, net_sales DESC
    LIMIT 10
  ```

***
