SheetSense

Google Sheets QUERY with select, where, and group by

QUERY runs a SQL-like statement on a range in Google Sheets, so you can filter, group, and sort data with one formula.

fx
=QUERY(Sales!A1:D,"select B, sum(D) where A >= date '2026-01-01' group by B order by sum(D) desc label sum(D) 'Revenue'",1)

The sheet: Sales tab: column A is Order date, column B is Region, column C is Rep, column D is Revenue, with headers in row 1.

How it works, step by step

  1. 01Sales!A1:D is the data range. Leaving off the last row number means new rows are included. The 1 at the end tells QUERY that row 1 is headers.
  2. 02where A >= date '2026-01-01' keeps orders from 2026 onward. Dates in QUERY need the date keyword and a yyyy-mm-dd string.
  3. 03group by B groups the rows by Region, and sum(D) adds Revenue within each group.
  4. 04order by sum(D) desc sorts regions from highest to lowest revenue, and label sum(D) 'Revenue' renames the output header from sum Revenue to Revenue.
  5. 05The result spills into a two-column table with one row per region.

The same logic in four tools

Excel
=GROUPBY(Sales!B2:B5000,Sales!D2:D5000,SUM,0,0,-2,Sales!A2:A5000>=DATE(2026,1,1))
Google Sheets
=QUERY(Sales!A1:D,"select B, sum(D) where A >= date '2026-01-01' group by B order by sum(D) desc label sum(D) 'Revenue'",1)
SQL
SELECT region, SUM(revenue) AS revenue
FROM sales
WHERE order_date >= DATE '2026-01-01'
GROUP BY region
ORDER BY revenue DESC;
Python (pandas)
import pandas as pd

result = (
    sales[sales["order_date"] >= "2026-01-01"]
    .groupby("region", as_index=False)["revenue"]
    .sum()
    .sort_values("revenue", ascending=False)
)

Mistakes that break it

  • !QUERY refers to columns by letter (A, B, D), or by Col1, Col2 when the data is an array in braces. Header names do not work, so select Region fails with a parse error.
  • !Each column gets one data type, chosen by the majority of its values. Values of the minority type come back blank, so a few text entries in a number column vanish from the sums.
  • !Text in a where clause needs single quotes (where B = 'West'), and a date from a cell has to be built into the string: "where A >= date '"&TEXT(F1,"yyyy-mm-dd")&"'".
  • !Excel has no QUERY function. The Excel formula shown uses GROUPBY, which is only in current Microsoft 365 builds. Older versions need a PivotTable or SUMIFS against a list of regions.

Common questions

Can I use the Google Sheets QUERY function in Excel?
No. Excel has no QUERY function. In Microsoft 365, GROUPBY and FILTER cover most of the same work, and Power Query handles larger jobs.
How do I reference a cell in a QUERY where clause?
Join the cell value into the query string. For text use "where B = '"&F1&"'", and for a number use "where D > "&F2.
Why does QUERY return blank cells for some values?
QUERY assigns one data type per column based on the majority of values. Values of the minority type, such as text IDs in a mostly numeric column, come back blank until the column is made consistent.

Got a version of this with your own ranges?

Paste it into the free translator and get the same breakdown for your exact formula. To run it against a real file, upload the CSV and describe the formula you need in a sentence.