Google Sheets
QUERY Function in Google Sheets
QUERY runs a Google Visualization API query on a range of data — a SQL-like language that filters, sorts, aggregates, and pivots directly inside a sheet.
Syntax
=QUERY(data, query, [headers])Parameters
dataRequiredThe range of cells to run the query against.
queryRequiredA query string written in Google's query language.
headersOptionalNumber of header rows at the top of the data range.
Examples
Filter and sort
=QUERY(A1:D100, "SELECT A, B, D WHERE C > 100 ORDER BY D DESC", 1)Selects columns A, B, D where column C > 100 and sorts by D descending.
Group and aggregate
=QUERY(A1:C100, "SELECT A, SUM(C) WHERE B = 'West' GROUP BY A", 1)Totals column C per unique value in A, restricted to West.
Common Errors
- #VALUE!
Cause: Query references a column letter that doesn't exist in the data range.
Fix: Use column letters relative to the data range (A is the first column of `data`, not the sheet).
Best Practices
- Quote string literals with single quotes inside the query string.
- Keep the data range tight — QUERY re-runs whenever the range changes.
Frequently Asked Questions
Need a custom version of this formula?
Describe what you need in plain English and get a ready-to-use formula in seconds.
Generate this formula with XLFormula AI