ORDER BY
Use ORDER BY to sort results by one or more columns, each ascending or descending. Set the sort direction for each column, and rely on the query's ordering behavior when you combine ORDER BY with TIMESERIES. You can sort by columns from SHOW or by generated columns that clauses like WITH and COMPARE TO add.
Syntax
Anchor to Sort directionSort 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.
ShopifyQL
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
FROM customers SHOW new_customer_records GROUP BY month SINCE startOfYear(0y) UNTIL today ORDER BY month ASCDescription
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
FROM sales SHOW net_sales GROUP BY sales_channel, product_title DURING last_month ORDER BY sales_channel ASC, net_sales DESC LIMIT 25
Anchor to Ordering behaviorOrdering behavior
The order a query returns depends on whether it uses TIMESERIES, ORDER BY, or both:
- When a query uses
TIMESERIESwithoutORDER BY, results are ordered by the time dimension. - When a query uses
TIMESERIESwithORDER BY, the time dimension stays the primary order, thenORDER BYcolumns break ties. - When a query uses
ORDER BYwithoutTIMESERIES, rows are sorted by the specified dimensions or metrics. - When a query has neither
TIMESERIESnorORDER BY, results are ordered by the firstSHOWcolumn.
ShopifyQL
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
FROM sales SHOW net_sales GROUP BY sales_channel TIMESERIES day DURING last_week ORDER BY net_sales DESCDescription
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
FROM sales SHOW net_sales GROUP BY sales_channel DURING last_month ORDER BY net_sales DESCDescription
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
FROM sessions SHOW sessions GROUP BY session_device_type DURING last_monthDescription
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
FROM sessions SHOW sessions TIMESERIES day DURING last_week
Anchor to Sorting generated columnsSorting generated columns
ORDER BY can sort the columns that other clauses generate, such as comparison columns from COMPARE TO and totals or cumulative columns from WITH. Reference a generated column by the name that the generating clause defines.
ShopifyQL
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
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 10Description
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
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