With headers in A1:C1 and records in A2:C, sort a Google Sheets QUERY result by column B with =QUERY(A1:C, "select A, B, C order by B asc", 1). The order by clause sorts the formula’s output; it does not rearrange the original rows.
What QUERY sorting does
Google Sheets’ QUERY function selects, filters, groups, and sorts data using a query-language string. To sort its returned rows, add order by followed by the column to sort on and, optionally, the direction. Use asc for ascending order and desc for descending order.
This is useful when you want a sorted view that updates as the source data changes. For example, an inventory list can show the largest stock quantities first without changing the order of the source table. Because the formula creates a separate result, you can keep the original data in its current order and use the output for a report or dashboard.
order by B ascsorts column B from low to high, or alphabetically for text.order by B descsorts from high to low, or reverse alphabetically for text.- Adding more columns after the first gives you secondary and further sort keys.
Basic QUERY syntax and column references
The general function pattern is QUERY(data, query, [headers]). The data argument is the range to work with; the query argument is text containing instructions such as select, where, and order by. The optional headers argument tells Sheets how many rows at the top of the range are column headings.
For a normal sheet range, refer to columns by their sheet letters. If the input range is A:C, use A, B, and C in the query. The header count is separate from the column reference: setting it to 1 says the first row of the supplied range contains headers; it does not mean that the query should sort by its first column.
When a query uses a constructed array rather than a regular range, column references can instead use identifiers such as Col1 and Col2. If a formula reports that a column cannot be found, check whether its data argument is a sheet range or an array, then use the appropriate column identifiers for that input.
Sort by one column
Suppose a table has Item in column A, Category in B, and Units in C. To put the smallest unit counts first, with headers in A1:C1 and data beginning in row 2, enter =QUERY(A1:C, "select A, B, C order by C asc", 1). To show the largest counts first with the same layout, use =QUERY(A1:C, "select A, B, C order by C desc", 1).
Ascending is often the natural choice for names, dates from earliest to latest, or quantities from smallest to largest. Descending is useful for ranking, such as showing the largest sales figures first. Naming the direction explicitly makes the formula easier to read and helps prevent uncertainty about the intended order.
For a text column, the same clause sorts alphabetically or in reverse alphabetical order. For dates, the source cells should contain actual date values, not date-looking text. When a column mixes numbers, dates, and text, QUERY may interpret values according to the predominant type; values of other types can be treated as null. That can produce a result that looks as if some records have gone missing or sorted unexpectedly.
Sort by multiple columns
For a multi-level sort, list the most important key first, then add other keys separated by commas. With headers in A1:C1 and records below them, =QUERY(A1:C, "select A, B, C order by B asc, C desc", 1) sorts categories alphabetically and, within each category, places the largest unit count first.
The first key determines the overall groups. Each next key only orders records that tie on the preceding key. If two rows have the same category and units but you need a predictable order, add a final key such as the item name: with the same A:C layout, =QUERY(A1:C, "select A, B, C order by B asc, C desc, A asc", 1) uses the name to break those remaining ties.
Think through the priority before building a long clause. For a staff list, for example, you might sort by department ascending, then surname ascending, then start date descending. Each additional key should resolve a meaningful tie, rather than merely make the formula more complicated.
Combine sorting with selecting and filtering
A QUERY can filter rows before presenting them in sorted order. The clauses have a required sequence: put where after select, then put order by after the filter. With product names in A, categories in B, and numeric quantities in C, and headers in row 1, =QUERY(A1:C, "select A, B, C where C > 0 order by C desc", 1) returns rows with positive quantities, largest quantity first.
You can also return only the columns a report needs. With the same A:C layout, =QUERY(A1:C, "select A, C where C > 0 order by C desc", 1) displays the item and quantity while omitting category. The sort column does not necessarily have to be one of the displayed columns, but including it can make the output easier to understand.
To exclude blank values in the sort column, use a condition such as where B is not null. For example, with records in A2:C and a header in A1:C1, =QUERY(A1:C, "select A, B, C where B is not null order by B asc", 1) omits rows where B is empty before sorting. This is often clearer than relying on where blanks happen to appear in a sorted result.
Headers, expanding ranges, and output space
The third argument is optional, but setting it explicitly can make a formula more reliable. If the data argument begins with one header row, use 1; if it contains no header row, use 0. If you leave the argument out, Sheets guesses how many header rows the range has, which may not match an unusual or inconsistently formatted table.
An open-ended range such as A1:C can include new records added below the existing data, so the query can update without changing its range. It also covers many empty rows, however. If the sheet contains unrelated data farther down those columns, or if performance becomes an issue in a very large workbook, use a suitable bounded range instead and extend it as needed.
QUERY returns an array of results that spills into cells below and to the right of the formula. Keep that output area clear. If existing values or merged cells block the result, Sheets may show an array-expansion error. Move the formula, clear the obstructing cells, or choose a destination with enough room; changing the sort clause will not resolve a blocked output.
Common problems and how to fix them
- Unexpected column identifier: Check the data argument. Regular sheet ranges generally use sheet column letters, while constructed arrays may need identifiers such as
Col1. - The header appears as a data row: Check the third argument. A range with one heading row should generally use
1; a range containing only records should use0. - Numbers appear in an odd order: Confirm that the source values are numbers rather than text. Mixed data types in a QUERY column can also affect which values are recognized as that column’s type.
- The output seems incomplete: Look for a filter condition that excludes rows, blank values in the sort column, or mixed types. Also check that the source range covers all the records you expect.
- The formula cannot expand: Clear cells in the destination area that the result needs to occupy, and check for merged cells there.
- Tied rows appear in an unexpected order: Add a secondary sort key. Rows equal on every stated key have no additional ordering instruction in the query.
When editing the query string, keep its clauses in the right order and make sure text values inside the query use single quotation marks. The formula itself uses double quotation marks around the whole query string. For instance, a condition matching a text value is written inside that string as where B = 'Tools'. A missing or misplaced quote is a frequent source of formula parse errors.
QUERY sort compared with SORT and menu sorting
Use QUERY when sorting is one part of a larger operation, such as selecting particular columns, filtering rows, or grouping data. The query string can express these operations together, and its result remains a separate, automatically updated view.
For a straightforward formula-driven sort without query language, Google Sheets also has the SORT function. With a complete table in A2:C100, no header included, and the second column of that range as the sort key, =SORT(A2:C100, 2, TRUE) returns the rows in ascending order. SORT uses the sort key’s position within the supplied range; in this example, 2 means the second column of A2:C100, which is column B. It can be a simpler option when you do not need QUERY’s filtering or column selection.
For a one-time rearrangement of the original table, select the range and use Data > Sort range > Advanced range sorting options. The menu offers sort-key and direction choices and can identify a header row. Unlike either formula, this changes the selected data’s row order, so include all columns that belong together in each record. Sorting only one column can separate values from their associated names or other fields.
These approaches solve different problems: QUERY and SORT generate formula results, while the menu sort changes the source range itself. Choose based on whether you need a live view or a permanent reordering of the existing rows.
Frequently Asked Questions
How do I sort a QUERY result in descending order?
Put order by and the relevant column in the query string, followed by desc. For a range A1:C with headers in row 1, for example, =QUERY(A1:C, "select A, B, C order by C desc", 1) sorts by column C from highest to lowest.
Can Google Sheets QUERY sort by two columns?
Yes. Put both sort keys after order by, separated by a comma, and specify the direction for each. The first key sets the main order; the second orders rows that tie on the first.
Does QUERY sort the original data?
No. QUERY displays a separate result in the cells where its formula is placed. To rearrange the source rows themselves, use the sheet’s sort menu after selecting the full record range.
Why does QUERY sort numbers or dates incorrectly?
Check whether the source cells contain genuine numeric or date values rather than text that only looks like a number or date. Also check for mixed types in the column: QUERY can treat values that differ from the predominant type as null.
Why is my QUERY result not appearing?
First check the formula syntax, header count, and column references. Then look for existing content or merged cells in the area where the array result needs to expand. A filter may also be excluding the rows you expected to see.
Related
- Insert a Table of Contents in Word and Keep It Updated
- Insert a Virtual Signature in Word: Steps and Options
- Insert a Section Into a Table of Contents in Microsoft Word
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Schedule a Meeting in Outlook Email: Step-by-StepTo schedule a meeting from an Outlook email, create a calendar meeting, add the people you want to…
- Excel Force Page Breaks: How to Insert and Control ThemTo force a page break in Excel for Microsoft 365 on Windows, select the row below where a…
- Date Formula in Google Sheets: Dates, Days, and UsesThe basic date formula in Google Sheets is =DATE(year, month, day). For example, with the year, month, and…
- How to Make a Monthly Budget in Google Sheets Step by StepTo make a monthly budget in Google Sheets, create separate income and expense tables, enter planned and actual…
- Excel Shortcut Keys: Essential Windows and Mac TipsExcel shortcut keys are keyboard combinations that let you edit cells, format data, navigate a worksheet, and manage…
- How to Insert a Line in Word Using Your KeyboardTo insert a horizontal line in Microsoft Word with your keyboard, put the cursor in an empty paragraph,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.