QUERY(data, query, [headers])One free generation or explanation per browser, shared across Excel and Google Sheets. Account usage limits apply afterward.
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.
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.
Imagine you are managing a sales dataset stored in a Google Sheets spreadsheet:
| SaleID | ProductName | Quantity | Price |
|---|---|---|---|
| 1 | Widget A | 3 | 25.00 |
| 2 | Widget B | 5 | 30.00 |
| 3 | Widget C | 2 | 15.00 |
You want to find out the total sales for each product.
To achieve this, you would use the QUERY function as follows:
=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.When you apply the QUERY formula to the Sales Data Table, the output would look like this:
| ProductName | TotalSales |
|---|---|
| Widget A | 75.00 |
| Widget B | 150.00 |
| Widget C | 30.00 |
This result shows the total sales for each product based on the quantity sold and the price.
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.
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!
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.
Sign up for Better Analyst and get unlimited AI-powered formula generation, explanations, and advanced data analysis.
Browse our complete library of Google Sheets formula guides
Compare your options
Open a neutral research prompt in the AI tool you already use. Compare capabilities, limitations, and fit before you choose.
AI answers can be incomplete. Check source links, current pricing, and product documentation before deciding.