To sort query results in ascending order in Google Sheets, use the ORDER BY clause with the ASC keyword inside the QUERY function, for example: =QUERY(A1:C10, "SELECT * ORDER BY B ASC", 1). This sorts rows by column B from smallest to largest (or A to Z for text).
What the QUERY Function Does
Google Sheets’ QUERY function is a powerful tool that lets you run SQL-like queries on your data. It uses the Google Visualization API Query Language, which is similar to SQL but with some differences. The basic syntax is:
=QUERY(data, query_string, [headers])
- data – the range you want to query (e.g., A1:C100).
- query_string – the instructions in quotes (e.g., “SELECT * ORDER BY B ASC”).
- headers – optional; the number of header rows in your data (default is 1).
When you include ORDER BY in the query string, you control the sort order of the returned rows. Adding ASC sorts ascending (A to Z, smallest to largest), while DESC sorts descending.
How ORDER BY ASC Works
The ORDER BY clause sorts the result set based on one or more columns. For ascending order, you write ASC after the column identifier. For example:
=QUERY(A1:C20, "SELECT * ORDER BY B ASC", 1)
This returns all rows from A1:C20, sorted by column B in ascending order. If column B contains numbers, 1 comes before 2; if text, ‘apple’ comes before ‘banana’. Dates sort chronologically.
You can sort by multiple columns—just list them separated by commas. For instance, ORDER BY A ASC, B DESC sorts by A ascending, then by B descending within ties.
Choosing the Right Column and Data Type
Make sure the column you sort by contains consistent data types. Mixing text and numbers in the same column can cause unexpected results because the sort treats them differently. For example, if column B has both text and numbers, the query may return an error or sort oddly. To avoid issues, ensure the column is formatted consistently (e.g., all numbers as numbers, not text).
Also, note that the column letters in the query refer to the columns in the data range you specified, not the entire sheet. If your data range is B2:D20, then column B in the sheet is column A in the query. To sort by the sheet’s column C, you’d use ORDER BY B (since B is the second column in the range).
Common Problems and Solutions
Here are frequent issues when using ORDER BY ASC:
- Error: “Unable to parse query string” – Usually a typo in the query string. Check for missing commas, incorrect column letters, or missing quotes.
- Sorting by header row – If your data has a header row, set the third argument in QUERY to 1. Otherwise, the header might be sorted as data.
- Text sorting case-sensitive – By default, text sorts case-insensitively in QUERY, but if you need case-sensitive sorting, you can use
ORDER BY LOWER(B)to force lowercase comparison. - Numbers stored as text – If numbers are left-aligned, they might be text. Use
VALUE()to convert, or fix the data format.
Alternatives to QUERY for Sorting
While QUERY is powerful, sometimes simpler functions work better:
- SORT function:
=SORT(A1:C20, 2, TRUE)sorts by the second column ascending. It’s easier for basic sorts but doesn’t filter or aggregate. - Data > Sort sheet: Use the menu to sort manually, but it doesn’t update automatically.
- FILTER and SORT combined:
=SORT(FILTER(A1:C20, B1:B20>10), 2, TRUE)filters then sorts.
QUERY is best when you need to combine sorting with filtering, grouping, or aggregation in one formula.
Performance and Best Practices
QUERY can slow down large datasets. To keep it fast:
- Limit the range to the actual data, avoiding full-column references like A:Z.
- Use
LIMITto restrict rows if you only need the top few. - Combine with
WHEREto filter before sorting, reducing the data processed.
Example with LIMIT: =QUERY(A1:C100, "SELECT * ORDER BY B ASC LIMIT 10", 1) returns only the 10 smallest rows by column B.
Frequently Asked Questions
Can I sort ascending by a column that contains dates? Yes, dates are stored as numbers in Google Sheets, so ORDER BY B ASC sorts chronologically from earliest to latest.
How do I sort ascending but ignore blank cells? Use a WHERE clause: =QUERY(A1:C20, "SELECT * WHERE B IS NOT NULL ORDER BY B ASC", 1).
What if I want to sort by a column that is not in the SELECT list? You can sort by any column in the data range, even if you don’t include it in SELECT. For example: =QUERY(A1:C20, "SELECT A, C ORDER BY B ASC", 1).
Does ORDER BY ASC work with text that has numbers like “Item 2”? It sorts lexicographically, so “Item 10” comes before “Item 2”. To sort numerically, you’d need to extract the number.
Can I use ORDER BY ASC in a pivot table? Pivot tables have their own sort options, not QUERY. Use the pivot table’s sort settings instead.
Why does my QUERY return an error when I use ORDER BY with a column that has mixed data types? Mixed types cause errors because the sort cannot compare text and numbers. Clean your data first.
Conclusion
Sorting ascending with QUERY is straightforward: add ORDER BY column ASC to your query string. Remember to account for headers, data types, and performance. With practice, you’ll combine it with other clauses to build dynamic reports.
Related
- Random QR Code: How to Get One Safely
- Windows Shortcut for CMD: Quick Keys & How to Use Them
- Formal Invoice Template Excel: How to Make One
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Power Query Load from Folder: Full GuideTo load data from a folder in Excel Power Query, use Data > Get Data > From File…
- Conditional Formatting Based on Text in Excel: RulesYes. In Excel for Microsoft 365, select the cells you want to format, then choose Home > Conditional…
- Compare Two Documents in Word: Step-by-Step GuideTo compare two separate documents in Word, open the desktop app and choose Review > Compare > Compare.…
- New Outlook Calendar: How to Change Calendar ViewsTo change the view in New Outlook Calendar, open Calendar, select View on the toolbar (or use the…
- How to Add Page Numbers in Word Online Using a BrowserTo add page numbers in Word Online, open your document in Word for the web, select Insert >…
- What Is a Split Queen Sheet? Size and Fit ExplainedA split queen sheet is bedding made for a queen-size bed whose sleeping surface is divided into two…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.