To sum values in Google Sheets using the QUERY function, use =QUERY(data, "select sum(column)", 1), where data is the range and column is the letter of the numeric column you want to total. For example, =QUERY(A1:B10, "select sum(B)", 1) sums all values in column B for rows 1–10 and returns a single number. This works in any web browser on Google Sheets; in Microsoft Excel, the equivalent is the SUMIFS or SUMPRODUCT function, since Excel does not have QUERY.
What Is the QUERY Function?
QUERY is a Google Sheets function that lets you run a SQL-like query on a range of data. It takes three arguments: the data range, a query string written in Google Visualization API Query Language, and an optional header row count. The function can filter, sort, group, pivot, and aggregate data. For summing, you use the sum() aggregation inside a select clause. It is one of the most powerful functions in Google Sheets because it combines the logic of several other functions (like SUMIF, AVERAGEIF, and FILTER) into one formula.
How to Use QUERY to Sum a Column
The basic syntax is =QUERY(range, "select sum(column)", headers). Here is a step-by-step example:
- Assume you have sales data in columns A (product) and B (amount), with headers in row 1 and data in rows 2–20.
- In an empty cell, type
=QUERY(A1:B20, "select sum(B)", 1). - Press Enter. The result is the total of all values in B2:B20. For instance, if B2:B20 contains 10, 20, 30, the result is 60.
You can also sum an entire column without a header: =QUERY(A1:B20, "select sum(B)", 0) (use 0 if there is no header row). The query string is case-insensitive, but column letters must match the range.
Summing with Conditions: WHERE and GROUP BY
Often you need to sum only rows that meet a condition. Use the where clause. For example, to sum sales for the product “Widget” (column A), use:
=QUERY(A1:B20, "select sum(B) where A = 'Widget'", 1)
Note the single quotes around text values. For numbers, no quotes are needed: where B > 100. To sum by category, use group by. For instance, to get total sales per product:
=QUERY(A1:B20, "select A, sum(B) group by A", 1)
This returns a two-column table: product names and their sums. The group by clause is required whenever you select a non-aggregated column alongside an aggregation.
Common Errors and How to Fix Them
Several mistakes trip up users new to QUERY:
- #VALUE! error: Usually means the query string is malformed. Check for missing commas, unmatched quotes, or a misspelled column letter.
- #REF! error: The range is invalid or the column letter does not exist in the range. For example, if your range is A:B, you cannot select column C.
- Text in numeric column: If the column you are summing contains text, QUERY may ignore it or return an error. Ensure the column is truly numeric.
- Header count mismatch: If you set headers to 1 but the range has no header, QUERY will treat the first data row as a header and exclude it from the sum. Adjust the third argument to 0 or 1 accordingly.
QUERY vs. SUMIFS: Which Should You Use?
For simple conditional sums, SUMIFS is often simpler and faster. For example, =SUMIFS(B2:B20, A2:A20, "Widget") gives the same result as the QUERY with WHERE. However, QUERY is more flexible when you need to group by multiple columns, sort the results, or combine multiple aggregations in one output. QUERY also handles dynamic criteria more gracefully, and it can output a full table that updates automatically. Use SUMIFS for a single condition or a few conditions; use QUERY for complex reporting or when you want to avoid helper columns.
Performance and Large Data Sets
QUERY can handle tens of thousands of rows, but performance degrades with extremely large datasets (over 100,000 cells). For large data, consider using a pivot table or the SUMIFS function, which are optimized. Also, QUERY recalculates every time the data changes, which can slow down your sheet. To improve performance, limit the range (e.g., A1:B1000 instead of A:B) and avoid using QUERY on entire columns when possible.
Frequently Asked Questions
Can I sum multiple columns in one QUERY? Yes, use select sum(A), sum(B) if both columns are in the range. For example, =QUERY(A1:C10, "select sum(A), sum(B)", 1) returns two sums side by side.
How do I sum only unique values? QUERY does not have a direct “sum unique” function, but you can combine it with UNIQUE: =SUM(QUERY(UNIQUE(A1:B10), "select sum(Col2)", 1)) – note that UNIQUE returns an array, and you need to use Col1, Col2, etc., in the query string.
Why does my QUERY return a header row? By default, QUERY includes a header row if you set the third argument to 1. To remove it, set the third argument to 0, or wrap the result in another function like =INDEX(QUERY(...), 2) to skip the header.
Can I use QUERY to sum with a date range? Yes, use a WHERE clause with date comparisons. For example, =QUERY(A1:C10, "select sum(C) where A >= date '2023-01-01' and A <= date '2023-12-31'", 1) sums column C for dates in 2023. Dates must be in yyyy-mm-dd format and enclosed in single quotes.
Does QUERY work in Excel? No, QUERY is exclusive to Google Sheets. In Excel, use SUMIFS, SUMPRODUCT, or Power Query (Get & Transform) for similar tasks.
What is the difference between sum and total in QUERY? There is no “total” function in QUERY; use sum() for numeric columns. For text columns, you can use count() to count non-empty cells.