The QUERY function is arguably the most powerful tool in Google Sheets. It allows you to use SQL-like commands to filter, sort, and manipulate large datasets with a single formula. Instead of using multiple filters or complex nested IF statements, you can use QUERY to pull exactly the data you need into a new table.
Step 1: Understand the Basic QUERY Syntax
The QUERY function follows a specific structure that you must follow for it to work correctly. The syntax is: =QUERY(data, query, [headers]).
- data: The range of cells you want to analyze (e.g., A1:E100).
- query: The actual command (written in plain text inside quotation marks) that tells Google Sheets what to do.
- headers: (Optional) A number indicating how many header rows your data has.
Step 2: Select Specific Columns
The most basic use of the QUERY function is selecting specific columns from a dataset. If you only want to see columns A and C from a large table, your formula would look like this: =QUERY(A1:E100, "SELECT A, C"). This tells the function to ignore columns B, D, and E entirely.
Step 3: Filter Data with the WHERE Clause
To pull data that meets specific criteria, use the WHERE clause. For example, if you have a sales list in column B and you only want to see rows where the sale is greater than $500, use: =QUERY(A1:E100, "SELECT A, B, C WHERE B > 500"). Note that text values must be enclosed in single quotes (e.g., WHERE C = 'Completed').
Step 4: Sort Results Using ORDER BY
Instead of manually sorting your data, the QUERY function can do it automatically. Use ORDER BY followed by the column letter. To sort sales from highest to lowest, the formula would be: =QUERY(A1:E100, "SELECT A, B ORDER BY B DESC"). Use ASC for ascending order and DESC for descending order.
Step 5: Group and Aggregate Data
If you want to summarize data (like finding the total sales per employee), use GROUP BY along with an aggregation function like SUM, AVG, or COUNT. For example: =QUERY(A1:E100, "SELECT A, SUM(B) GROUP BY A"). This will list every unique name in Column A and provide the total sum of their values in Column B.
Step 6: Troubleshooting Common QUERY Errors
If your formula returns a #VALUE! or #N/A error, check these three things: 1. Ensure your query string is wrapped in double quotes. 2. Make sure your column letters are capitalized in the query string. 3. Check that your data types are consistent; the QUERY function often fails if a single column contains both numbers and text.
💡 Pro Tip: Keep your software updated to avoid these issues in the future.
Category: #Software