Google Sheets QUERY Function: Syntax, Examples & Tips

The QUERY function retrieves specific data from a range based on criteria, allowing for filtering, sorting, and aggregating results.
Google Sheets
QUERY(data, query, [headers])

One free generation or explanation per browser, shared across Excel and Google Sheets. Account usage limits apply afterward.

On this page
  1. Parameters
  2. What is a QUERY Function?
  3. A Practical Example
  4. QUERY Formula
  5. Result of the Formula
  6. Why Use QUERY?
  7. Key Takeaways:
  8. Example use case

Parameters

data:
The range of cells that contains the data you want to query. This can be a single range or multiple ranges.

query:
A string that specifies the query to execute on the data. This includes the SELECT statement and any conditions or ordering.

headers:
An optional parameter that indicates the number of header rows in the data. If omitted, Google Sheets will try to determine the number of header rows automatically.

What is a QUERY Function?

The QUERY function in Google Sheets is a powerful tool that allows users to retrieve and manipulate data from a range or array using a SQL-like syntax. It enables users to perform complex data analysis and filtering without needing to write extensive formulas. The QUERY function is particularly useful for those who are familiar with database queries, as it provides a more intuitive way to work with data.

In simpler terms, QUERY allows you to ask questions about your data and get answers in a structured format.

A Practical Example

Imagine you are managing a sales dataset stored in a Google Sheets spreadsheet:

Sales Data Table:

SaleIDProductNameQuantityPrice
1Widget A325.00
2Widget B530.00
3Widget C215.00

You want to find out the total sales for each product.

QUERY Formula

To achieve this, you would use the QUERY function as follows:

Google Sheets
=QUERY(A2:D, "SELECT B, SUM(C*D) GROUP BY B", 1)

In this formula:

  • A2:D is the range of your sales data.
  • "SELECT B, SUM(C*D) GROUP BY B" is the query string that specifies which columns to select and how to aggregate the data.
  • 1 indicates that the first row contains headers.

Result of the Formula

When you apply the QUERY formula to the Sales Data Table, the output would look like this:

ProductNameTotalSales
Widget A75.00
Widget B150.00
Widget C30.00

This result shows the total sales for each product based on the quantity sold and the price.

Why Use QUERY?

QUERY is beneficial because it simplifies data analysis and reporting in Google Sheets. It allows users to perform complex calculations and filtering with a simple and readable syntax. Additionally, it can handle large datasets efficiently, making it a valuable tool for data analysis.

Key Takeaways:

  • QUERY: A powerful function that allows users to retrieve and manipulate data using SQL-like syntax.
  • Data Analysis: Ideal for performing complex calculations and filtering without extensive formulas.
  • Efficiency: Handles large datasets effectively, making it suitable for data analysis tasks.
  • Common Use Cases: Useful for sales analysis, inventory management, and any scenario where data retrieval and manipulation are necessary.

QUERY is an essential function for anyone working with data in Google Sheets, providing a powerful way to enhance data analysis and reporting capabilities.

Happy spreadsheeting!

Example use case

You have a sales data table with columns for product IDs, sales amounts, and dates. Use the QUERY function to filter and summarize total sales for a specific product over a selected date range, enabling quick insights into sales performance.

Need more help with Google Sheets formulas?

Sign up for Better Analyst and get unlimited AI-powered formula generation, explanations, and advanced data analysis.

Explore all Google Sheets formulas

Browse our complete library of Google Sheets formula guides

Compare your options

Decide with evidence, not a sales pitch.

Open a neutral research prompt in the AI tool you already use. Compare capabilities, limitations, and fit before you choose.

  1. 01ChatGPTOpen a balanced comparison prompt in ChatGPT.
  2. 02ClaudeOpen the same research prompt in Claude.
  3. 03PerplexityResearch the comparison with linked web sources.

AI answers can be incomplete. Check source links, current pricing, and product documentation before deciding.