QUERY Function
The most powerful function in Google Sheets. Uses a SQL-like query language to filter, sort, group, pivot, and aggregate data — all within a formula.
Google Sheets
Syntax
QUERY(data, query, [headers])Parameters
| Parameter | Description |
|---|---|
| data | The range of cells to query. |
| query | The query string using Google Visualization API Query Language (similar to SQL). |
| headers(optional) | Number of header rows in the data. -1 for auto-detect. |
Examples
Select specific columns where region is East
=QUERY(A1:D100, "SELECT A, C WHERE B = 'East'", 1)Result: Returns columns A and C for rows where column B is 'East'
Group by category and sum sales
=QUERY(A1:C100, "SELECT A, SUM(C) GROUP BY A ORDER BY SUM(C) DESC", 1)Result: Sales totals grouped by category, sorted highest first
Top 10 records by value
=QUERY(A1:D100, "SELECT * ORDER BY D DESC LIMIT 10", 1)Result: Returns the top 10 rows sorted by column D
Tips & Best Practices
- •QUERY is Google Sheets only — no Excel equivalent (use Power Query in Excel instead).
- •Column references use letters (A, B, C...) not column names in the query string.
- •Use the LABEL clause to rename output headers: "SELECT A, SUM(B) LABEL SUM(B) 'Total'".
- •Combine with IMPORTRANGE to query data from other spreadsheets.
- •For date comparisons: "WHERE A > date '2024-01-01'".
Related Formulas
Need a different formula?
Describe what you need in plain English and our AI will generate the formula for you.
Try Formula Generator