The QUERY function in Google Sheets is a versatile tool that lets you filter, sort, and analyze data using simple SQL-like commands. It helps you quickly extract meaningful insights from large datasets by combining multiple operations into a single formula.
QUERY Function in Google Sheets
The QUERY function in Google Sheets is a powerful tool that allows you to filter, sort, and analyze large datasets with ease. It uses SQL-like syntax to help you retrieve specific data from a selected range, making it ideal for data analysis and reporting tasks.
How It Works
The QUERY function works by retrieving data from a specified range and applying SQL-like operations, such as sorting, filtering, and aggregating.
- Data Range: The range or array of data to query (e.g., A1:D100).
- Query String: The SQL-like query that defines what data to select and how to process it (e.g., "SELECT A, B WHERE C > 100").
- Headers (optional): Defines the number of header rows in your data for proper column reference.
QUERY Function Syntax in Google Sheets
The syntax of the Google Sheets QUERY statement is:
=QUERY(data, query, [headers])Parameters:
- data: The range of cells or array to query (e.g., A2:D10).
- query: The SQL-like query string that specifies the data selection, filtering, or aggregation criteria.
- headers (optional): The number of header rows in your dataset to help identify column names.
How to Use the QUERY Function in Google Sheets
To use Query function Google Sheets follow the steps given below:
Step 1: Organize Your Data
Make sure your data is organized in a tabular format with clear column headers.

Step 2: Open the QUERY Function
- Select the cell where you want to display the query result.
- Type the following formula:
=QUERY(data_range, query, [headers])where:
- data_range is the range of cells containing your data (e.g., A2:D6).
- query is the SQL-like query you want to perform.
- headers is the optional number of header rows (usually set to 1 if you have column headers).

Step 3: Write the Query String
- The query string follows an SQL-like syntax. You can use various operations, such as SELECT, WHERE, ORDER BY, and GROUP BY.
- For example, if you want to get all products from the Electronics category with sales greater than 500, use the following query:
=QUERY(A2:D6, "SELECT A, C WHERE B = 'Electronics' AND C > 500")This query:
- Selects columns A (Product) and C (Sales).
- Filters for Electronics products and sales greater than 500.

Step 4: Press Enter and View Results
After entering the query formula, press Enter. Google Sheets will display the results in the selected cell. The data will be filtered, sorted, or aggregated based on the conditions you specified.

Examples of QUERY Function Usage:
Here’s how to use the QUERY function with SELECT, WHERE, GROUP BY, ORDER BY, LIMIT, and LABEL based on the data provided:
Using ORDER BY
=QUERY(A2:D6, "SELECT A, C ORDER BY C DESC")This will select columns A and C, and sort them in descending order based on Sales.

Using GROUP BY
=QUERY(A2:D6, "SELECT B, SUM(C) GROUP BY B")This will sum the sales in column C, grouped by product category in column B.

Using SELECT
The SELECT clause lets you choose specific columns from the dataset.
Example: Display only the Product, Category, and Sales columns:
=QUERY(A1:D6, "SELECT A, B, C", 1)
Using LIMIT
The LIMIT clause restricts the number of rows returned.
Example: Display only the top 3 rows with the highest sales:
=QUERY(A1:D6, "SELECT A, B, C, D ORDER BY C DESC LIMIT 3", 1)
Using LABEL
The LABEL clause renames column headers in the output.
Example: Rename SUM as "Total Sales":
=QUERY(A1:D6, "SELECT B, SUM(C) GROUP BY B LABEL SUM(C) 'Total Sales'", 1)
Combining Clauses
You can combine multiple clauses for more complex queries.
Example: Display the top 2 products in "Electronics" with the highest sales:
=QUERY(A1:D6, "SELECT A, C WHERE B = 'Electronics' ORDER BY C DESC LIMIT 2", 1)