Google Sheets QUERY.
D2What is QUERY?
The QUERY function runs a SQL-like query over a range in Google Sheets. It is the most powerful way to filter, sort, and aggregate data in a single formula — and it has no direct equivalent in Excel.
=QUERY(data, query, [headers]) - data — the range to query, e.g.
A:D. - query — a query string in Google Visualization API Query Language (the SQL-like part).
- [headers] — optional number of header rows.
The query clauses
Clauses must appear in this order: SELECT → WHERE → GROUP BY → PIVOT → ORDER BY → LIMIT → LABEL.
Examples
1. Select rows that match a condition
=QUERY(A:D, "SELECT * WHERE B = 'Sales'") 2. Pick columns and sort
=QUERY(A:D, "SELECT A, C WHERE C > 100 ORDER BY C DESC") 3. Group and aggregate
=QUERY(A:D, "SELECT B, SUM(D) GROUP BY B") 4. Limit the results
=QUERY(A:D, "SELECT A, D ORDER BY D DESC LIMIT 5") 5. Query data from another sheet (IMPORTRANGE)
=QUERY(IMPORTRANGE("url", "Sheet1!A:D"), "SELECT Col1, Col4 WHERE Col4 > 100") With IMPORTRANGE, reference columns as Col1, Col2… instead of letters.
Common errors
"Unable to parse query string"
The query syntax is wrong — a misspelled clause, wrong column reference (letter vs Col1), missing space, or double quotes used where single quotes are needed around text values. Recheck the clause order.
Text values must use single quotes
Inside the query string, wrap text in single quotes: WHERE B = 'Sales', not double quotes (the double quotes already delimit the whole query string).
Generate QUERY formulas with FormulaAI
Describe what you want — "show all rows where department is Sales, sorted by revenue, top 10" — pick Google Sheets, and FormulaAI returns the complete QUERY formula with the clauses in the right order. Pick Excel instead and it returns a FILTER or Power Query alternative.
Frequently asked questions
What does the QUERY function do in Google Sheets?
QUERY runs a SQL-like query over a range of data. You give it the data range and a query string with clauses like SELECT, WHERE, ORDER BY, and GROUP BY, and it returns the matching rows and columns. It is one of the most powerful functions in Google Sheets and has no direct equivalent in Excel.
How do I reference columns inside QUERY?
When the data is a plain range like A:D, use the column letters (SELECT A, C WHERE C > 100). When the data comes from IMPORTRANGE or another function, use Col1, Col2, Col3 instead of letters (SELECT Col1, Col3 WHERE Col3 > 100).
Why does QUERY return 'Unable to parse query string'?
That error means the query syntax is wrong — often a misspelled clause, the wrong column reference (letter vs Col1), a missing space, or quotes around a text value that should use single quotes. Check the clause order: SELECT, then WHERE, then GROUP BY, then ORDER BY, then LIMIT.
Can I use QUERY with data from another spreadsheet?
Yes. Wrap IMPORTRANGE inside QUERY: =QUERY(IMPORTRANGE('url','Sheet1!A:D'), 'SELECT Col1, Col4 WHERE Col4 > 100'). Remember to use Col1, Col2 style references because the data is imported.
Does Excel have a QUERY function?
No. QUERY is Google Sheets-only. The closest Excel equivalents are Power Query, FILTER, and database functions like DSUM. FormulaAI can generate the right alternative when you pick Excel as the target platform.
Related guides
- Google Sheets Formulas Guide — QUERY, ARRAYFORMULA, IMPORTRANGE and more.
- SUMIFS with Multiple Criteria — conditional sums.
- FAQ — more about FormulaAI.