Google Sheets Function Guide

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])

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:

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

Related Guides & Tools: Explore FILTER & UNIQUE Dynamic Lists, SUMIFS Multiple Criteria, GROUPBY & LET Dynamic Reporting, or download ready-made workbooks from the Templates Pack.

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:

=QUERY(A1:D6, "SELECT B, SUM(D) WHERE C = 'Paid' GROUP BY B ORDER BY SUM(D) DESC LABEL B 'Category', SUM(D) 'Paid Total'", 1)
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
SEO & Logic Tip: Always wrap QUERY in IFERROR(..., "No matching rows") when filtering on user-selected dropdown values so empty results stay clean.