How to sort data with QUERY in Google Sheets
Google Sheets formula
=QUERY(A1:F13,"select C, B, F order by C, F desc",1)Add an order by clause: =QUERY(A1:F13,"select C, B, F order by C, F desc",1) sorts by region, then by revenue from largest within each region.
- Result on the example
- a 13-row list that spills from H1 in
H1 - Excel
- See below
See it work
The formula is already in H1. Change any input and the result updates, or edit the formula to experiment.
How it works
order by lists the sort keys in priority order. C sorts the regions A to Z (asc is the default), and F desc breaks ties within each region by revenue, largest first. The header row stays on top because the last argument says there is one.
=QUERY(A1:F13,"select C, B, F order by C, F desc",1)Result{13 rows × 3 cols} "Region", "Rep", "Revenue", "East", …
A1:F1313 rows × 6 cols: "Date", "Rep", "Region", "Product", …A range: 13 rows × 6 columns.
QUERY(A1:F13,"select C, B, F order by C, F desc",1){13 rows × 3 cols} "Region", "Rep", "Revenue", "East", …QUERY: Runs a SQL-like query: select, where, group by, order by, limit, label.- data
A1:F1313 rows × 6 cols: "Date", "Rep", "Region", "Product", … - query
"select C, B, F order by C, F desc" - headers
1
- data
In Excel
=SORT(CHOOSECOLS(A2:F13,3,2,6),{1,3},{1,-1}).Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =QUERY(A1:F13,"select C, B, F order by F desc",1) | spills 13 × 3Different result | One key: every order by revenue, largest first. Sorted by revenue only. |
| =SORT({C2:C13,B2:B13,F2:F13},1,TRUE,3,FALSE) | spills 12 × 3Different result | SORT does the same without the header row. No header row. |
Common mistakes
- order by comes after where and group by but before limit and label. In any other place QUERY returns #VALUE!.
- You can order by a column you don't select, but in a grouped query you must order by a grouped column or an aggregate, such as
order by sum(F) desc.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Sort data with QUERY
H1, use QUERY to return Region, Rep and Revenue sorted by region A to Z, and by revenue from largest to smallest within each region.Questions
How do I sort by a total?
Order by the aggregate: select C, sum(F) group by C order by sum(F) desc.
How do I sort alphabetically, ignoring case?
Order by the lower-case value: order by lower(B).
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine.