QUERY Function in Google Sheets
Use QUERY to filter, sort, group and total a range with a small SQL-like language ā all in one formula.
=QUERY(A1:D100, "SELECT A, B WHERE C > 100", 1)
Returns columns A and B for rows where column C is over 100. The trailing 1 says the data has one header row.
How it works
QUERY runs a query-language string against a range. Refer to columns by their letters (A, B, Cā¦). Common clauses: SELECT which columns, WHERE to filter, ORDER BY to sort, GROUP BY with aggregates like SUM(), and LIMIT. Text values in WHERE go in single quotes; dates use date 'yyyy-mm-dd'.
Variations
Filter by a text value
=QUERY(A1:D100, "SELECT A, D WHERE B = 'West'", 1)
Single quotes around the text you match.
Sort the results
=QUERY(A1:D100, "SELECT A, C ORDER BY C DESC", 1)
DESC for high-to-low, ASC for low-to-high.
Total by group (like a pivot)
=QUERY(A1:D100, "SELECT B, SUM(C) GROUP BY B", 1)
Sums column C for each distinct value in column B.
Reference a cell in the query
=QUERY(A1:D100, "SELECT A WHERE C > "&E1, 1)
Join the number in E1 with & to make the filter dynamic.
Examples
| Scenario | Formula |
|---|---|
| Top 5 rows by amount | =QUERY(A1:D100, "SELECT A, C ORDER BY C DESC LIMIT 5", 1) |
| Rows in a date range | =QUERY(A1:D100, "SELECT * WHERE A >= date '2026-01-01'", 1) |
| Count rows per category | =QUERY(A1:D100, "SELECT B, COUNT(A) GROUP BY B", 1) |
FAQ
How do I use a cell value in QUERY?
Concatenate it into the query string with &: =QUERY(A:D, "SELECT A WHERE C > "&E1, 1). Wrap text values in single quotes: "...B = '"&E1&"'".
Why does QUERY say column headers are wrong?
Set the third argument (header rows). Use 1 when your data has one header row, 0 when it has none.
Can QUERY pull from another sheet?
Yes ā reference the other sheet's range: =QUERY(Sheet2!A1:D100, "SELECT A, B", 1), or wrap IMPORTRANGE for another workbook.