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