# How to sort data with QUERY in Google Sheets

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.

**Google Sheets:** `=QUERY(A1:F13,"select C, B, F order by C, F desc",1)`

**Excel:** Excel has no QUERY function. Use SORT on the chosen columns: `=SORT(CHOOSECOLS(A2:F13,3,2,6),{1,3},{1,-1})`.

## Example

Sample data (row 1 is the header):

| A | B | C | D | E | F |
| --- | --- | --- | --- | --- | --- |
| Date | Rep | Region | Product | Units | Revenue |
| 2026-07-03 | Ana Ruiz | East | Widget | 40 | 2080 |
| 2026-07-06 | Raj Patel | West | Gadget | 25 | 2225 |
| 2026-07-10 | Lee Chen | East | Gizmo | 12 | 1740 |
| 2026-07-14 | Ana Ruiz | North | Gadget | 30 | 2670 |
| 2026-07-17 | Kim Ortiz | West | Widget | 55 | 2860 |
| 2026-07-21 | Raj Patel | East | Widget Pro | 18 | 1422 |
| 2026-07-24 | Lee Chen | South | Gadget | 22 | 1958 |
| 2026-07-29 | Kim Ortiz | East | Gizmo | 9 | 1305 |
| 2026-08-04 | Mo Diaz | North | Gizmo | 14 | 2030 |
| 2026-08-07 | Raj Patel | South | Widget | 20 | 1040 |
| 2026-08-12 | Kim Ortiz | North | Gadget | 16 | 1424 |
| 2026-08-18 | Mo Diaz | East | Gizmo Mini | 28 | 1092 |

Formula in H1: `=QUERY(A1:F13,"select C, B, F order by C, F desc",1)`

Result (spills 13 × 3): Region, Rep, Revenue; East, Ana Ruiz, $2,080; East, Lee Chen, $1,740; East, Raj Patel, $1,422; East, Kim Ortiz, $1,305; East, Mo Diaz, $1,092; North, Ana Ruiz, $2,670; North, Mo Diaz, $2,030; North, Kim Ortiz, $1,424; South, Lee Chen, $1,958; South, Raj Patel, $1,040; West, Kim Ortiz, $2,860; West, Raj Patel, $2,225

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

## Other ways

- `=QUERY(A1:F13,"select C, B, F order by F desc",1)`: One key: every order by revenue, largest first. (Sorted by revenue only.)
- `=SORT({C2:C13,B2:B13,F2:F13},1,TRUE,3,FALSE)`: 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`.

## 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)`.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/sort-data-with-query
