# Google Sheets QUERY Function: Syntax, Examples & Tips

- URL: https://betteranalyst.com/google-sheets/query-function
- Author: Better Analyst

Formula Bot is now Better Analyst—same account, more powerful analysis.Formula Bot is now Better Analyst—same account and workflows, with more powerful analysis, dashboards, and automation.

[Learn what changed](https://betteranalyst.com/formula-bot-is-now-better-analyst)

The QUERY function retrieves specific data from a range based on criteria, allowing for filtering, sorting, and aggregating results.

```
QUERY(data, query, [headers])
```

Generate FormulaExplain Formula

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

Describe the QUERY you need in plain EnglishGenerate Formula

## 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:

| 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.

## QUERY Formula

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.

## Result of the Formula

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.

## 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.

[Create Free Account](https://betteranalyst.com/signup)[Try Formula Generator](https://betteranalyst.com/excel-ai)

## Google Sheets how-to guides

- [How To Count Unique Values In Google Sheets](https://betteranalyst.com/google-sheets/how-to-count-unique-values-in-google-sheets)
- [How To Create A Pivot Table In Google Sheets](https://betteranalyst.com/google-sheets/how-to-create-a-pivot-table-in-google-sheets)
- [How To Divide In Google Sheets](https://betteranalyst.com/google-sheets/how-to-divide-in-google-sheets)
- [How To Enable Macros In Google Sheets](https://betteranalyst.com/google-sheets/how-to-enable-macros-in-google-sheets)
- [How To Filter In Google Sheets](https://betteranalyst.com/google-sheets/how-to-filter-in-google-sheets)
- [How To Find Circular References In Google Sheets](https://betteranalyst.com/google-sheets/how-to-find-circular-references-in-google-sheets)
- [How To Find Duplicates In Google Sheets](https://betteranalyst.com/google-sheets/how-to-find-duplicates-in-google-sheets)
- [How To Freeze A Row In Google Sheets](https://betteranalyst.com/google-sheets/how-to-freeze-a-row-in-google-sheets)

## 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.](https://chatgpt.com/?prompt=Compare%20Better%20Analyst%20with%20using%20a%20general-purpose%20AI%20assistant%20for%20data%20analysis.%20Evaluate%20spreadsheet%20and%20document%20analysis%2C%20dashboards%2C%20repeatable%20reports%2C%20code%20transparency%2C%20data%20connections%2C%20pricing%2C%20and%20limitations.%20Use%20current%20public%20sources%2C%20distinguish%20verified%20facts%20from%20assumptions%2C%20and%20explain%20which%20types%20of%20users%20each%20option%20fits%20best.)
2. [02ClaudeOpen the same research prompt in Claude.](https://claude.ai/new?q=Compare%20Better%20Analyst%20with%20using%20a%20general-purpose%20AI%20assistant%20for%20data%20analysis.%20Evaluate%20spreadsheet%20and%20document%20analysis%2C%20dashboards%2C%20repeatable%20reports%2C%20code%20transparency%2C%20data%20connections%2C%20pricing%2C%20and%20limitations.%20Use%20current%20public%20sources%2C%20distinguish%20verified%20facts%20from%20assumptions%2C%20and%20explain%20which%20types%20of%20users%20each%20option%20fits%20best.)
3. [03PerplexityResearch the comparison with linked web sources.](https://www.perplexity.ai/search?q=Compare%20Better%20Analyst%20with%20using%20a%20general-purpose%20AI%20assistant%20for%20data%20analysis.%20Evaluate%20spreadsheet%20and%20document%20analysis%2C%20dashboards%2C%20repeatable%20reports%2C%20code%20transparency%2C%20data%20connections%2C%20pricing%2C%20and%20limitations.%20Use%20current%20public%20sources%2C%20distinguish%20verified%20facts%20from%20assumptions%2C%20and%20explain%20which%20types%20of%20users%20each%20option%20fits%20best.)

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