Query Order by Google Sheets: Master ORDER BY in QUERY

Learn to sort Google Sheets QUERY results with ORDER BY. This step-by-step guide covers syntax, multi-column sorting, filters, labeling, and common pitfalls for reliable data queries.

How To Sheets
How To Sheets Team
·5 min read
Quick AnswerSteps

To order results in Google Sheets with the QUERY function, use the ORDER BY clause inside the query string. Example: =QUERY(A2:D, "select A, B where C > 0 order by A asc", 1). You can extend to multiple columns and specify asc/desc for each. This guide covers syntax, practical examples, and troubleshooting.

Understanding QUERY and ORDER BY in Google Sheets

The QUERY function in Google Sheets lets you pull data from a range using a SQL-like syntax. The ORDER BY clause sorts the resulting rows, giving you control over how results appear without changing the source data. When combined with WHERE, you can filter and sort in a single pass, which is essential for reporting and dashboards. This section sets the foundation for effective use of query order by google sheets.

Excel Formula
=QUERY(A1:D100, "select A, B, C where D > 0 order by A asc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where D > 0 order by C desc, A asc", 1)

Breakdown:

  • select A, B, C returns the first three columns in the result
  • where D > 0 filters rows with a numeric column D
  • order by A asc sorts by A from A to Z
  • The final parameter 1 indicates there is one header row in the source

Variations:

  • You can sort by a column that isn’t shown in the output by using its Col reference or by including it in the SELECT, then sorting on it. You can also rename columns with the label clause:
Excel Formula
=QUERY(A1:D100, "select A, B where D > 0 order by A asc label A 'Name', B 'Value'", 1)

Basic ORDER BY syntax

The ORDER BY clause is the core for sorting results in a QUERY. It accepts one or more column references followed by ASC or DESC. If you omit ASC or DESC, Google Sheets defaults to ascending order. This simple pattern applies to both numeric and text data, but behavior depends on data type and headers. Using ORDER BY effectively enables prioritized views of large datasets.

Excel Formula
=QUERY(A1:D100, "select A, B where C > 10 order by A asc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where C > 10 order by B desc", 1)

Tips:

  • Use LIMIT to cap results after sorting:
Excel Formula
=QUERY(A1:D100, "select A, B where C > 10 order by A asc limit 25", 1)
  • When sorting by numeric columns with blanks, filter blanks to avoid skewed results:
Excel Formula
=QUERY(A1:D100, "select A, B where C > 0 and C is not null order by C asc", 1)

Ordering by a single column

Sorting by a single column is the most common use case. Decide which column defines the sort key and whether you want ascending or descending order. If your data includes mixed types (numbers and text), consider normalizing the column or applying a WHERE clause to constrain values before sorting. This approach is ideal for ranking, prioritizing, or alphabetizing reports.

Excel Formula
=QUERY(A1:D100, "select A, B, C where C > 0 order by A asc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where C > 0 order by B desc", 1)

Ordering by multiple columns and tie-breakers

Multi-column ORDER BY provides deterministic results when the primary sort key has duplicates. The syntax allows multiple column references separated by commas, each with its own ASC/DESC direction. This is useful for stable sorts where a secondary key refines the ordering (e.g., sort by date, then by name).

Excel Formula
=QUERY(A1:D100, "select A, B, C where C > 0 order by C asc, A desc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B, C where C > 0 order by C asc, B desc, A asc", 1)

Note that you can use more columns and mix directions to achieve the exact ordering you need.

Ascending vs descending and locale considerations

ASC sorts from smallest to largest; DESC reverses that order. When sorting textual data, Google Sheets uses lexicographic order; numbers sort numerically. If your data includes locale-specific characters, consider normalizing text (e.g., using UPPER or LOWER) or applying a locale-aware sorting strategy before querying. For very large datasets, pre-filtering with WHERE and limiting results can improve performance.

Excel Formula
=QUERY(A1:D100, "select A, B where D > 0 order by A asc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where D > 0 order by B desc", 1)

Filtering with WHERE and ORDER BY together

Combining WHERE with ORDER BY narrows results before sorting, which is especially useful for dashboards. You can filter by text using contains or matches, numeric ranges, or explicit values. Remember to quote string literals inside the query. Labels can rename output columns so the final display remains clean.

Excel Formula
=QUERY(A1:D100, "select A, B where C contains 'North' order by D desc label B 'Sales', D 'Date'", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where C = 'Active' order by A asc", 1)

Practical templates: dynamic range, named ranges, and headers

Named ranges and dynamic ranges make queries reusable across sheets. If you name a range (e.g., data), you must use Col1, Col2, etc., in your select list. For dynamic lengths, combine with INDIRECT or use a boundary like A1:D. This approach keeps your formulas robust when rows are added.

Excel Formula
=QUERY(data, "select Col1, Col2 where Col3 > 0 order by Col1 asc", 1)
Excel Formula
=QUERY(INDIRECT("data!A1:D" & COUNTA(data!A:A) + 1), "select Col1, Col2 order by Col2", 1)

Debugging common issues with QUERY ORDER BY

Common problems include miscounted header rows, incorrect column references, or attempting to order by a column not in the data range. Always verify the third argument (headers) matches your sheet, and ensure ORDER BY references columns within the selected range. If you see unexpected sorts, check data types and remove leading/trailing spaces in text.

Excel Formula
=QUERY(A1:D100, "select A, B where C > 0 order by A asc", 1)
Excel Formula
=QUERY(A1:D100, "select A, B where C > 0 order by E asc", 1) -- E is out of range, fix to D or include E in range

Performance tips and best practices

For large datasets, filter before sorting to minimize processed rows. Use explicit headers count to avoid header misinterpretation. Prefer stable sort orders by combining primary and secondary keys. Consider pre-aggregating data or using helper columns to simplify complex ORDER BY expressions. Always test on a representative sample before applying to full datasets.

Excel Formula
=QUERY(A1:D1000, "select A, B where C > 50 order by C asc, A desc limit 100", 1)
Excel Formula
=QUERY(A1:D1000, "select A, B where C > 50 order by B asc", 1)

Steps

Estimated time: 60-90 minutes

  1. 1

    Prepare dataset and headers

    Ensure your range includes headers and representative data. Decide which column is the primary sort key.

    Tip: Use a clean header row to avoid misinterpretation by QUERY.
  2. 2

    Write a basic query

    Create a simple QUERY formula that selects a couple of columns with a minimal WHERE filter.

    Tip: Start with one sort key to validate correctness before adding more.
  3. 3

    Add ORDER BY

    Append ORDER BY with ASC or DESC to control sort direction. Test both.

    Tip: When sorting numbers, ensure they are numeric in the range, not text.
  4. 4

    Extend to multiple sorts

    Include a secondary sort key after a comma for deterministic ordering.

    Tip: Secondary keys help resolve ties and produce stable results.
  5. 5

    Combine with labels and limits

    Use label to customize headers and LIMIT to cap results.

    Tip: Label helps maintain readable dashboards; LIMIT prevents overloading views.
  6. 6

    Debug and optimize

    Validate data types, header count, and range selections. Refine as needed.

    Tip: If sorting seems off, check for mixed data types in the sort column.
Pro Tip: Always align the header count (third parameter) with your actual data header rows.
Warning: Avoid sorting columns that contain merged cells; it can produce unpredictable results.
Note: When using named ranges, reference columns as Col1, Col2 in the query.
Pro Tip: Combine ORDER BY with WHERE to minimize the data that needs sorting.
Note: Test with a small sample before applying to the entire dataset.

Prerequisites

Required

Optional

  • A sample dataset with headers to practice on
    Optional
  • Optional: Named range setup for advanced templates
    Optional

Keyboard Shortcuts

ActionShortcut
Copy formulaCopy a QUERY formula from the editor to the worksheetCtrl+C
Paste as plain textPaste without formatting when importing formulasCtrl++V
Find in sheetQuickly locate references within the datasetCtrl+F
Open formula editorEdit an existing QUERY formula in-placeF2

FAQ

How do I order by multiple columns in Google Sheets QUERY?

Use multiple columns in the ORDER BY clause, separated by commas, and assign ASC or DESC to each. This defines primary and secondary sort keys to produce a stable, predictable order.

You can order by several columns by listing them after ORDER BY, separated by commas, and setting ASC or DESC for each one.

Can I order by a column that isn't shown in the results?

Yes. You can order by any column within the range, even if you do not SELECT it. The sort is based on the underlying column values.

Yes, you can sort by a column even if it isn’t in the final displayed columns.

What should I set for the header parameter in QUERY?

The third parameter specifies the number of header rows in the data. Use 1 if your data has a single header row, 0 if there are none.

Set the header count to match how many header rows you have, usually 1.

Why is my sort order different for text vs numbers?

Google QUERY sorts text lexicographically and numbers numerically. If values look misordered, verify the data type and remove mixed types in the same column.

Text sorts like a dictionary, numbers sort numerically, so mixed data types can change the order.

How can I rename the sorted output columns?

Use the label clause inside the QUERY string to rename output columns, improving readability in dashboards.

Use label to assign friendly names to the output columns.

The Essentials

  • Master ORDER BY to control result order
  • Use multiple sort keys for deterministic results
  • Combine WHERE with ORDER BY for efficient queries
  • Label and LIMIT to improve readability and performance
  • Validate headers and data types to avoid surprises

Related Articles