The QUERY function in Google Sheets allows users to run powerful queries on their datasets using a simple and intuitive syntax. By leveraging the Google Visualization API Query Language, this function enables sophisticated data manipulation, extraction, and analysis straight from your spreadsheets. With QUERY, users can filter, aggregate, and sort their data efficiently, making it an invaluable tool for users handling large amounts of information.
Syntax
QUERY(data, query, [headers])
- data: The range of cells containing the data you want to query.
- query: A string that specifies the query to be executed, which can include filtering and ordering clauses.
- headers: An optional parameter indicating the number of header rows in the data range.
Example #1
QUERY(A1:C10, "SELECT A, B WHERE C > 10", 1)
This function selects columns A and B from rows where the value in column C is greater than 10. Result: A list of entries that meet the criteria, such as Name: John, Age: 25, if the conditions are satisfied.
Example #2
QUERY(B1:D20, "SELECT B, SUM(D) GROUP BY B", 1)
This example groups the data in column B and sums the values in column D for each unique value in column B. Result: A summary table showing each category from B alongside their total from D.
Example #3
QUERY(E1:G50, "SELECT E ORDER BY G DESC", 0)
This function queries column E and orders the results in descending order based on the values in column G. Result: A ranked list of entries from E based on total scores from G.
Error handling
- REF! – This error occurs if the specified range is invalid, often due to deleted rows or columns.
- VALUE! – This happens when the input values are of the wrong type, such as text where numbers are expected.
- Query Error – This indicates there is an issue with the query string itself, such as incorrect syntax or unsupported functions.