Google Sheets QUERY: ORDER BY Multiple Columns, Tested

To sort a QUERY by more than one column, list the columns after order by, separated by commas, each with its own direction:

=QUERY(A1:F8, "select A, B, D order by A asc, D desc", 1)

That sorts by column A from A to Z and, inside each group of equal values in A, by column D from largest to smallest. This guide shows how that works on real data, how to sort by totals, and the three things that most often spoil a sorted QUERY: blank rows, the header row and a missing comma.

Tested in Google Sheets. On November 11, 2026 we ran every query on this page against the same seven-row sales table, including deliberately broken versions, and copied the result or the exact error message. How we test

Want the formula without writing it? Our QUERY formula generator builds the sort, the filters and the totals for you.

The sample data

A: Region B: Rep C: Date D: Amount E: Status
2 North Ana 2026-01-15 120 Paid
3 South Ben 2026-02-03 80 Open
4 North Cai 2026-01-20 250 Paid
5 East Ana 2026-03-01 80 Paid
6 South O’Neil 2026-01-05 40 Open
7 North Ben 2026-02-28 100
8 East Cai 2026-02-10 250 Paid

Two columns, two directions

order by A asc, D desc returned:

Region Rep Amount
East Cai 250
East Ana 80
North Cai 250
North Ana 120
North Ben 100
South Ben 80
South O’Neil 40

Column F held a score that none of these queries use. The second column only matters when the first one ties. Swap them and the meaning changes: order by D desc, B asc put both 250 rows first, then 120 and 100, then the two 80 rows with Ana before Ben, then 40.

  • asc is the default: order by A, D desc gave exactly the same result as order by A asc, D desc.
  • The keywords ignore case: DESC worked like desc.
  • Text sorts alphabetically, numbers by value and dates from oldest to newest. order by C desc listed March 1 first.

Sort by a column you don’t show

The sort column doesn’t have to be in select. select B order by D desc returned only the names, ordered by amount: Cai, Cai, Ana, Ben, Ben, Ana, O’Neil.

Sort by a total

With group by, sort by the aggregate exactly as you wrote it in select:

=QUERY(A1:F8, "select A, sum(D) group by A order by sum(D) desc", 1)

It returned North 470, East 330, South 120. The heading of the total column was “sum Amount”; add label sum(D) 'Total' at the end to rename it. That gave the same rows with the heading “Total”.

Every column in select must be grouped or inside an aggregate. select A, B, sum(D) group by A failed with ADD_COL_TO_GROUP_BY_OR_AGG: B.

Top N rows

Add limit after the sort: select B, D order by D desc limit 3 returned Cai 250, Cai 250 and Ana 120. In the query text, limit goes after order by.

Problem 1: blank rows at the top

Open-ended ranges such as A1:C are handy because new rows are included automatically. But the empty rows below your data are included too, and in an ascending sort they come first. On a five-row table in another tab, select A, C order by C over A:C returned 14 empty rows before the first item.

Filter them out on a column that is always filled:

=QUERY('Week 8 data'!A:C, "select A, C where A is not null order by C", 1)

That returned just the five items, from 5 to 50. Empty cells in a sorted column behave the same way: order by E put the row with no status first.

Problem 2: the header row sorted into the data

Found in testing: with 0 as the third argument, QUERY treats the first row as data. =QUERY(A1:F8, "select A, B order by A", 0) sorted the header “Region, Rep” in among the rows, between North and South. Use 1 when your range starts with a header row. If you leave the argument out, Google’s help page says QUERY guesses; writing 1 removes the guesswork.

Problem 3: syntax errors

Mistake What we got
Missing comma: order by A desc D PARSE_ERROR: Encountered ” <ID> “D “” at line 1, column 29. Was expecting one of: … “,”
A column that isn’t in the data: order by Z NO_COLUMN: Z
Lowercase letters: select a, d NO_COLUMN: a

The column number in a PARSE_ERROR counts characters in the query text, so it points at the exact spot.

When SORT is simpler

If you don’t need to filter or total, the SORT function does the same with column numbers instead of letters. =SORT(A2:E8, 1, TRUE, 4, FALSE) (column 1 ascending, then column 4 descending) returned the same order as our first QUERY. SORT takes the data without the header row.

Filtering before you sort

A where clause goes before order by. Quick rules, all tested on the same data:

  • Text in single quotes: where E = 'Paid'. Comparisons are case-sensitive: where A = 'north' found nothing, while where lower(A) = 'north' found the three North rows.
  • Dates with the word date: where C > date '2026-02-01'. Without it, we got no rows and no error.
  • Several conditions with and or or: where D >= 100 and E = 'Paid'.
  • A value from a cell is joined from outside the quotes: "select B, D where D > "&G1.

The QUERY formula generator page lists the other mistakes we ran into.

Related guides