Google Sheets QUERY Function: Select, Filter & Group Data
Use =QUERY(data, query, [headers]) to pick specific columns, filter rows, sort results, and build live summary tables from a single formula.
Sample Sales & Expense Table Used in This Guide
Suppose columns A:E on a tab named Orders contain the following rows:
| Col A: Order ID | Col B: Order Date | Col C: Category | Col D: Status | Col E: Amount ($) |
|---|---|---|---|---|
| ORD-101 | 2026-11-04 | Software | Paid | $120.00 |
| ORD-102 | 2026-11-09 | Hosting | Paid | $85.00 |
| ORD-103 | 2026-11-15 | Software | Pending | $240.00 |
| ORD-104 | 2026-11-19 | Marketing | Paid | $310.00 |
| ORD-105 | 2026-11-26 | Hosting | Overdue | $85.00 |
1. Basic Syntax & Choosing Columns (SELECT)
The QUERY syntax is:
=QUERY(data, "query_string", [headers])
data: Your cell range (such asOrders!A1:E100)."query_string": Clause string wrapped in double quotes. Column letters refer to letters within the range (A,B,C...), not necessarily sheet columns.[headers]: Number of header rows at the top ofdata(usually1).
To pull only Order ID, Category, and Amount for all rows:
=QUERY(Orders!A1:E100, "SELECT A, C, E", 1)
2. Filtering Rows With WHERE & ORDER BY
Use WHERE to keep matching rows and ORDER BY (with ASC or DESC) to sort the output:
- Text match (wrap text in single quotes inside the query string):
=QUERY(Orders!A1:E100, "SELECT A, B, E WHERE D = 'Paid' ORDER BY E DESC", 1) - Numeric threshold (no quotes around numbers):
=QUERY(Orders!A1:E100, "SELECT A, C, E WHERE E >= 100 ORDER BY E DESC", 1) - Multiple conditions with AND / OR:
=QUERY(Orders!A1:E100, "SELECT A, C, E WHERE D = 'Paid' AND E > 100", 1)
3. Referencing a Cell Dynamically Inside WHERE
Hard-coding 'Paid' works for a static view, but dashboards usually read a filter value from a cell like G2. Close the double quotes, concatenate the cell reference with &, and reopen quotes around the closing single quote:
=QUERY(Orders!A1:E100, "SELECT A, C, E WHERE D = '" & G2 & "' ORDER BY B ASC", 1)
If G2 contains Pending, Google Sheets evaluates the query string to "SELECT A, C, E WHERE D = 'Pending' ORDER BY B ASC" and returns ORD-103 ($240.00).
4. Summarizing Totals With GROUP BY
QUERY can aggregate numbers like a lightweight Pivot Table or SUMIFS table. Whenever you aggregate one column with SUM, COUNT, or AVG, list every non-aggregated selected column in GROUP BY:
=QUERY(Orders!A1:E100, "SELECT C, SUM(E), COUNT(A) WHERE C IS NOT NULL GROUP BY C ORDER BY SUM(E) DESC", 1)
For the sample table above, this outputs total spend and order count per category (Software: $360.00 across 2 orders; Marketing: $310.00 across 1 order; Hosting: $170.00 across 2 orders).
5. Filtering by Date in QUERY
Dates inside QUERY require the date 'YYYY-MM-DD' literal format, or a cell date formatted with TEXT(H2, "yyyy-mm-dd"):
=QUERY(Orders!A1:E100, "SELECT A, B, C, E WHERE B >= date '" & TEXT(H2, "yyyy-mm-dd") & "'", 1)
Common QUERY Errors & Quick Fixes
#VALUE!(Unable to parse query string): Usually caused by mismatched quotes, a typo in a clause keyword (likeWEREinstead ofWHERE), or forgetting single quotes around a text value.- Empty result / no rows matching: Check whether column
Ehas mixed text and numbers—QUERYinfers a column's dominant type, and mixed types can cause unexpected blanks. Clean imports first using our Convert Number Stored as Text Guide. - Need a fast diagnostic checklist? Open the interactive Error Debugger or customize formulas in the Formula Finder.
6. Worked QUERY Aggregation With Copyable Formula
Use QUERY when you want a single formula to filter rows, group by category, and sort totals without creating a Pivot Table manually:
| A | B | C | D | |
|---|---|---|---|---|
| # | Date | Category | Status | Amount ($) |
| 2 | 2026-12-01 | Software | Paid | $120.00 |
| 3 | 2026-12-02 | Hosting | Paid | $240.00 |
| 4 | 2026-12-03 | Software | Paid | $80.00 |
| 5 | 2026-12-04 | Hosting | Pending | $90.00 |
QUERY in IFERROR(..., "No matching rows") when filtering on user-selected dropdown values so empty results stay clean.