If you've ever struggled to find specific data in your spreadsheets, Google Sheets has a solution: the QUERY function. It is a search and filter tool that allows you to extract specific information in seconds, making it an essential skill for anyone looking to master query Google Sheets techniques.
In this article, we’ll explore the Google Sheets QUERY function, discuss where and how to use it, understand its syntax, and look at easy and more advanced examples, including the Google Sheets Query formula.
Note: This article was first published in March 2024 and was thoroughly revised and updated in May 2025 to provide the most accurate and comprehensive information.
The QUERY function in Google Sheets is a basic tool for data analysis that helps users extract specific information from large sets of data quickly.
Many professionals, like analysts, educators, and marketers, use the QUERY function to sort and filter data. For example, marketers can group customers by age or location to run targeted campaigns, while managers use it to track campaign performance and find areas for improvement.
This is just one example of how the QUERY function can be used. In fact, it has a wide range of filtering options that allow for even more complex analysis.
It is important to use the correct syntax in the QUERY function to avoid common errors. Mistakes, such as failing to specify data ranges properly or incorrectly using quotation marks, can lead to syntax errors in queries.
🎥 Prefer to watch and learn? Check out our in-depth video on mastering the Google Sheets QUERY function. This video complements the tips and tricks in this guide, offering a visual approach to efficient data management. Get started now and streamline your data tasks!
The Google Sheets QUERY function offers numerous benefits and advantages, making it a powerful tool for data analysis and manipulation. Here are some of the key benefits:
By leveraging these benefits, users can significantly enhance their data management capabilities in Google Sheets.
Whether you’re a marketer, SEO specialist, or someone who needs to use spreadsheets, here is how you can benefit from the QUERY function:
The QUERY function in Google Sheets helps you work with large datasets by filtering and sorting information based on specific criteria, such as showing only orders from a certain region or products within a price range. It uses SQL-like syntax to extract targeted data efficiently. You can also perform basic calculations, like summing sales or averaging prices, directly within your queries using arithmetic operators.
If you're running a website, you know how important it is to keep an eye on its performance. With QUERY, you can pull in data from Google Search Console to see how your site is doing, and analyze which pages are getting the most traffic, where your visitors are coming from, and what keywords they're using to find your site.
Creating a named range for the data table can help in pulling information using the Google Sheets Query function.
Backlinks are the votes of confidence for your website because they show search engines that other sites trust yours. With QUERY, you can examine your backlink profile to find sites that are linking to yours and evaluate their impact on your SEO efforts. Additionally, you can detect any low-quality or spammy links that may be negatively affecting your SEO performance and take corrective action.
And finally, you can analyze keyword data to identify the most relevant and high-performing keywords for your content. This includes finding search volume, competition level, and trends over time.
The QUERY function in Google Sheets has a simple syntax:
=QUERY(data, query_string, [headers])
QUERY reads your dataset and returns the result based on your question in a clean, structured list. It also treats each column as having a single data type, like text or numbers. If a column mixes types, Google Sheets uses the one that appears most frequently when running the query.
In the QUERY function, data selection refers to choosing the specific range or columns of cells that contain the information you want to analyze. This could be a single column, a row, or a larger block of data spanning multiple rows and columns.
When creating a QUERY, make sure you are formulating precise questions or instructions that help you extract the exact information you need from your data.
Here are some tips for crafting effective queries:
💡 If manual processes are causing you trouble, consider using a tool to automate data retrieval and overcome the limitations of the standard VLOOKUP formula with IF statements. Explore our comprehensive guide on using VLOOKUP with IF statements in spreadsheets.
Headers are the top rows in your data that describe what each column contains, like "Sales Representative," "Region," or "Sales Amount."
By setting header preferences in the QUERY function, you can specify how many rows at the top of your data should be treated as header rows.
If your headers are in the first row of your data, you'd set this parameter to 1. If your headers span multiple rows, you'd set it accordingly.
The QUERY_STRING in the QUERY function has several components, which allow you to define what data you want to see, how to summarize it, and perform basic calculations.
Aggregate functions like SUM, COUNT, AVG, MAX, and MIN help summarize large datasets by performing calculations across groups of rows. You can apply these functions to specific columns to get totals, averages, minimums, or maximums, making it easier to extract meaningful statistics and insights directly from your data.
Example: SELECT SUM(B) WHERE A = 'Category 1' calculates the total sum of values in column B where the value in column A is 'Category 1'.
You can perform basic arithmetic operations (+, -, *, /) within your queries to manipulate your data.
Example: SELECT A + B adds the values in columns A and B for each row.
Clauses are parts of the query that determine what you’re looking for. The “WHERE” clause filters your data based on certain conditions.
Example: WHERE A > 100 selects rows where the value in column A is greater than 100.
The FORMAT clause allows you to control how your query output appears. You can format numbers, dates, and times to ensure the data is displayed neatly and consistently.
Example: FORMAT B 'MM/DD/YYYY'
This formats column B as a date in MM/DD/YYYY format, improving readability without changing the actual values.
💡Tired of manually combining data in your spreadsheets? Discover a powerful tool that can automate and streamline your data management. Learn how to use the CONCATENATE function in Google Sheets to effortlessly merge data. Check out our comprehensive guide on leveraging CONCATENATE with queries in Google Sheets to boost your productivity.
Here are a few handy tips to make sure that your QUERY function runs without any issues.
1. Clauses are not case-sensitive, so you can write them in upper or lower case (e.g., select or SELECT).
2. Use column letters in uppercase, otherwise, you’ll get errors.
3. If you use more than one of the keywords in your query, remember to use them in the following order:
Diving into QUERY functions can feel overwhelming at first, so we’ve prepared a free spreadsheet template that includes all the examples and use cases from this article. Use it to practice and understand how QUERY works by trying it out yourself.
Here, we’ve put together the 10 most common ways people use the QUERY function in Google Sheets. These examples cover everything you might need when working with your data, from organizing and filtering to digging deep and finding important information.
Let's say you want to create a report that lists the names of all the sales representatives. To do this, you can use the QUERY function with the SELECT clause to create the column containing the names of the sales representatives.
Here's the QUERY function for this purpose:
=QUERY(B2:D17, "SELECT B")
Continuing with the example, let’s say you also want to include the “Region” and “Sales Amount ($)” columns in your report to provide context for each sales representative’s performance.
For this, you have to modify the QUERY function:
=QUERY(B2:D17, "SELECT B, C, D")
Additionally, in the QUERY function, you can specify column letters in the SELECT clause in any order you choose, such as SELECT B, D, C, or SELECT D, B, C. However, it's important to remember that each column letter should be mentioned only once within the SELECT clause to avoid duplication and ensure a clear output structure in your query results.
In Google Sheets, =QUERY(B2:D17, "SELECT B, C, D") explicitly selects columns B to D, while using a wildcard "*" for the given range will achieve the same output. The asterisk is a quick way to include all columns in the range without listing them individually. This is particularly handy when dealing with many columns, as it saves time and avoids the need to list each column individually.
The alternative formula will look like this:
=QUERY(B2:D17, "SELECT *")
To make the output more readable, you can rename the columns using the LABEL clause:
=QUERY(B2:D17, "SELECT B, C, D LABEL B 'Region', C 'Sales Amount ($)'")
But what if you want to focus on reviewing the sales made by a specific sales representative? To evaluate the contribution of John Smith to the overall sales in the Northeast region, use the WHERE clause:
=QUERY(B2:D17, "SELECT * WHERE B = 'John Smith'")
This function will return the following result, showing only the sales made by John Smith:
ORDER BY clause may be useful when reviewing the sales performance of your team across different regions. To identify the top-performing sales representatives based on their total sales amount and rank them in descending order, use this function:
=QUERY(B2:D17, "SELECT * ORDER BY D DESC")
It will return the following result, sorting the sales data by the "Sales Amount ($)" column in descending order:
If you need to present only the top 5 sales records from your sample data set to focus on the most significant sales contributions and provide a concise summary of the highest-performing representatives, use the LIMIT clause:
=QUERY(B2:D17, "SELECT * ORDER BY D DESC LIMIT 5")
This function will return the following result, sorting the sales data by the "Sales Amount ($)" column in descending order and limiting the output to the top 5 records:
The QUERY function in Google Sheets allows you to perform standard mathematical operations on numeric columns. Let’s calculate the percentage of sales for each sales representative. For convenience, we've added a total sales number in cell D18 using the standard Google Sheet formula SUM.
Now we need to divide the sales amount of each sales representative by the total sales figure, and then multiply that by 100 to calculate the percentages. This is what the calculation looks like as a QUERY formula:
=QUERY(B2:D17, "SELECT B, (D / " & D18 & ") * 100")
Sometimes, your original column names may not be very clear or presentable. To improve the readability of your output, you can use the LABEL clause in the QUERY function to rename columns with more descriptive headers.
=QUERY(B2:D17, "SELECT B, C, D LABEL B 'Sales Rep', C 'Sales Region', D 'Sales Amount'")
By applying this function, you change the column names into “Sales Rep,” “Sales Region,” and “Sales Amount”:
To determine the total sales amount generated by all sales representatives, use the Aggregate function:
=QUERY(B2:D17, "SELECT SUM(D)")
By using the Google Sheets QUERY GROUP BY clause to group the sales data based on sales team members, you can calculate the total sales amount for each sales representative separately:
=QUERY(B2:D17, "SELECT B, SUM(D) GROUP BY B")
If the task is to calculate the average sales amount per sales representative to evaluate individual performance within the team, apply the following formula:
=QUERY(B2:D17, "SELECT B, AVG(D) GROUP BY B ORDER BY AVG(D) DESC")
Using the AVG function and ORDER BY clause in this formula, helps you show the average sales amount and sort them in descending order. This allows you to see sales representatives who made more sales on average at the top of your list.
If you want to summarize sales by each representative across different regions, the PIVOT clause can help you transform rows into columns for easier analysis.
Unlike GROUP BY, the PIVOT clause automatically aggregates rows that share the same pivot value. This makes your output more compact and easier to read.
=QUERY(B2:D17, "SELECT B, SUM(D) WHERE B IS NOT NULL GROUP BY B PIVOT C")
Additionally, if you're interested in expanding your data manipulation toolkit, we recommend reading our article on using VLOOKUP with IF statements.
Now that we've covered the basic clauses for the QUERY function, let's examine more advanced scenarios to improve your data analysis skills further:
This formula combines all the data together from different tabs into one table. If you want to combine data from two tabs and then perform a query on it, you need to first consolidate the data using curly brackets "{}" to create an array, and then you can apply the QUERY function on this array. Assuming you have the same table structure on two sheets named "Tab1" and "Tab2," you can combine the data using the following function:
=QUERY({'Tab1'!B2:D9; 'Tab2'!B3:D9}, "SELECT *")
If you want to merge information from different tabs and get the total sales from each region, sorted in the descending order, here is the advanced formula you can apply:
=QUERY({'Tab1'!B2:D9; 'Tab2'!B3:D9}, "SELECT Col2, SUM(Col3) GROUP BY Col2 ORDER BY SUM(Col3) DESC")
It groups sales by region, adds them up, and sorts them from most profitable regions down.
Note that when you’re mixing data from different tabs and creating an array like this, the columns are referred to as Col1, Col2, etc., because Google Sheets doesn’t recognize the column letters (like A, B, C), only the relative position within the array.
To retrieve sales data for the Northeast region with sales amounts greater than $5000, add multiple criteria to your function:
=QUERY(B2:D17, "SELECT * WHERE C = 'Northeast' AND D > 5000")
If you need to import data from a completely different spreadsheet located in a different directory, you need to add the full file path in the formula and connect the spreadsheets. The first time the destination spreadsheet pulls data from a new source spreadsheet, permission needs to be granted.
To pull data on sales in the Northeast region from another spreadsheet where the data is stored in a tab named "Another sheet", adjust the function:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1QmL7aB5c4AL_u4Oxda_fmkq5osMeZjZj0PCf-rJJ1xA/edit#gid=797074713", "'Another sheet'!B2:D17"), "SELECT * WHERE Col2 = 'Northeast'")
The QUERY function in Google Sheets mimics Structured Query Language (SQL), making it easier for users familiar with SQL to manipulate data effectively.
Similar to using QUERY from another spreadsheet, you can also use it to pull data from multiple spreadsheets. For instance, imagine you're managing sales data in one spreadsheet and bonus information in another. By using QUERY, you can consolidate data from both spreadsheets to analyze sales performance and bonus payouts comprehensively.
To pull data on sales and bonuses of John Smith from 2 different spreadsheets, change the function:
=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/1rBJgspAu0JOo03NGcEGTsGoHsDZ4u6Crc4X06LyLTac/edit#gid=797074713", "Sales!B2:D17"), IMPORTRANGE("https://docs.google.com/spreadsheets/d/1CN58bJN1SEC7XvbSA1HTib1HzbRUaxiPmmsn-4qH20U/edit#gid=797074713", "Bonuses!B2:D17")}, "SELECT Col1, Col3, Col6 WHERE Col1 = 'John Smith'")
If you desire a cleaner look for your results, you can slightly modify the formula to summarize data in columns 3 and 6 and then group them by column 1. Here is how this upgraded formula works:
=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/1rBJgspAu0JOo03NGcEGTsGoHsDZ4u6Crc4X06LyLTac/edit#gid=797074713", "Sales!B2:D17"), IMPORTRANGE("https://docs.google.com/spreadsheets/d/1CN58bJN1SEC7XvbSA1HTib1HzbRUaxiPmmsn-4qH20U/edit#gid=797074713", "Bonuses!B2:D17")}, "SELECT Col1, SUM(Col3), SUM(Col6) WHERE Col1 = 'John Smith' GROUP BY Col1")
This small modification will create a single row about John Smith, including his sales and bonuses totals.
When working with the QUERY function in Google Sheets, you may experience various issues that can disrupt your data analysis.
This happens when the conditions in the query are too restrictive, resulting in no matching results. To resolve this, review and adjust the query conditions to ensure they accurately reflect the data you're trying to pull.
The #REF error means an invalid data reference or a conflict with cells in the range. This error commonly happens when referencing cells that have been deleted or moved. To fix it, double-check the data in your QUERY function and ensure they accurately point to the needed range.
Parsing errors occur when the query string within the QUERY function is incorrect or missing essential components. Incorrect or missing WHERE clauses can lead to parsing errors by failing to filter records based on specified conditions.
To troubleshoot this issue, carefully review the query syntax and ensure it follows the correct format. Verify that all keywords, operators, and arguments are properly selected.
This error appears when trying to select a column that is outside the available range of the data. This happens when referencing columns beyond the range specified in the data source. To resolve this, verify the column references in your QUERY formula and ensure they fall within the correct range of data.
To optimize the performance of the QUERY function, follow these best practices:
By following these best practices, users can optimize the performance of the QUERY function, ensuring that their data analysis and manipulation tasks are completed efficiently and effectively.
Google Sheets is equipped with a robust set of formulas that significantly elevate your ability to manipulate data and derive meaningful insights:
If you find yourself fighting with inaccurate or outdated data, you're not alone – especially if you're not a tech-savvy data analyst. But luckily there are specialized tools that will gather and prepare your data, leaving you to comfortably navigate it.
With the OWOX: Reports, Charts & Pivots Extension, you can effortlessly import BigQuery data directly into your Google Sheets. No more headaches with manual imports or messy data transfers. With the most accurate data in your hands and our detailed guide on QUERY functions, you can have all the tools you need to work with numbers effectively.
The QUERY function in Google Sheets allows you to retrieve specific data from a dataset by using SQL-like queries. It enables filtering, sorting, and performing calculations on data, making it a powerful tool for analyzing and managing large sets of information efficiently.
You can use the QUERY function by including sorting and filtering criteria within the query string. For example, you can use the "ORDER BY" clause to sort data based on a specific column, and the "WHERE" clause to filter data based on certain conditions.
You can use the QUERY function for SEO and marketing to analyze website traffic data, track keyword performance, and evaluate the effectiveness of marketing campaigns.
Yes, the QUERY function can handle data from multiple sheets or tabs in Google Sheets. You can reference data from different sheets by specifying the sheet name and range within the query string, allowing you to consolidate and analyze data from various sources in a single query.
Common QUERY errors include "No results found" (due to restrictive conditions), "#REF" (from invalid references), parsing errors (from incorrect syntax), and "NO_COLUMN" (when referencing columns outside the range). These can usually be fixed by reviewing your syntax, data references, and query conditions.