QUERY Function

The QUERY function in Google Sheets allows you to run SQL-like queries on your spreadsheet data. It is a powerful tool for filtering, sorting, and summarizing data dynamically without manually creating multiple formulas.

1. What is the QUERY Function?

The QUERY function uses a structured query language (similar to SQL) to retrieve specific data from a range.

Syntax:
=QUERY(data, query, [headers])

  • data — The range of cells to query
  • query — The query string in quotation marks, e.g., "SELECT A, B WHERE B>100"
  • headers — (Optional) Number of header rows in the range

2. Simple Example

=QUERY(A1:C10, "SELECT A, B WHERE B>100")

  • Selects columns A and B
  • Includes only rows where column B is greater than 100

3. Common QUERY Clauses

SELECT — Choose which columns to display
WHERE — Filter rows based on conditions
ORDER BY — Sort the data ascending (ASC) or descending (DESC)
LIMIT — Restrict the number of rows returned
GROUP BY — Aggregate data by a column
LABEL — Rename column headers in the output

Example:
=QUERY(A1:D20, "SELECT A, SUM(C) WHERE B='Sales' GROUP BY A ORDER BY SUM(C) DESC", 1)

  • Groups data by column A
  • Sums values in column C where column B is “Sales”
  • Sorts results by total sales in descending order

4. Benefits of QUERY Function

Combine filtering, sorting, and aggregation in one formula
Reduces the need for multiple helper columns
Handles large datasets efficiently
Enables dynamic reporting and dashboards

5. Tips for Using QUERY

Use single quotes ' for text values in conditions
Match column letters correctly (A, B, C, etc.)
Combine with ARRAYFORMULA or IMPORTRANGE for dynamic datasets
Test queries step by step to avoid syntax errors

Conclusion

The QUERY function in Google Sheets is a versatile tool for advanced data analysis.

Mastering QUERY allows you to filter, sort, summarize, and analyze data dynamically, making it ideal for reporting, dashboards, and complex spreadsheet tasks.

Home » GOOGLE SHEETS FOR BUSINESS (GSB) > Advanced Functions > QUERY Function