
Google Sheets Query is a powerful tool that allows you to extract and manipulate data from a spreadsheet with ease. You can use it to filter, sort, and aggregate data, making it an essential function for any spreadsheet user.
To get started with Google Sheets Query, you need to understand the basics of the function, which is where the "Basic Query Syntax" comes in. This syntax is the foundation of any query, and it's essential to understand how it works.
The basic syntax of a query is SELECT column_name FROM table_name WHERE condition. This syntax is straightforward and easy to use, but it's also limited in its capabilities. For example, if you have a table with multiple columns and you want to filter data based on multiple conditions, you'll need to use the "WHERE" clause multiple times.
Using the "WHERE" clause multiple times can lead to complex queries, which is where the "Advanced Query Functions" come in. These functions allow you to perform more complex operations, such as filtering data based on multiple conditions, using arrays, and more.
Broaden your view: Google Sheet Count How Many Times a Value Appears
Google Sheets Query Basics
The Google Sheets Query function has 9 clauses, each with a unique purpose, and they're optional, so you don't have to use all of them in one query.
The order of these clauses is crucial, and they must be written in this order: SELECT, WHERE, GROUP BY, PIVOT, ORDER BY, LIMIT, OFFSET, LABEL, and FORMAT.
You can use multiple space-separated clauses in one query string.
To understand how the QUERY function works, consider a sample data set of students enrolled in various courses at a university.
The QUERY function is entered in cell H1 and displayed in the function bar.
To extract data from columns B and D, you'll need to write a QUERY function.
Each clause has a specific purpose, and they can be used in various combinations to design data queries.
The sample data set is used to demonstrate the QUERY function in upcoming examples.
If this caught your attention, see: Google Spreadsheet Query Group by
Query Syntax
The Google Sheets Query function syntax can be a bit tricky to grasp at first, but don't worry, I've got you covered. The syntax is made up of three main parts: the data range, the query, and the headers.
See what others are reading: Gsheets Query
The data range is the range of cells that you want to analyze, and it's required. For example, in Example 1, the data range is A1:D234.
The query is the string inside the quotes that tells the function what to do, and it's also required. You can write your query using the Google Visualization API Query Language, which is similar to SQL. In Example 1, the query statement is "select B, D" which tells the function to select columns B and D from the data.
You can also refer to a cell with the inquiry written in the Google Query language, as shown in Example 2. The query string must be enclosed in double quotation marks.
The headers argument is optional and is used to define the number of header rows in your data set. If you omit this argument, Google Sheets will assume it based on the contents of your table.
Here's a breakdown of the query syntax:
- data – a set of cells that you want to request Google Sheets to perform an inquiry on.
- query – a string that contains an inquiry composed using the Google API Query Language.
- headers – an optional part of the Query formula to define the number of heading rows in your data set.
For example, the query formula in Example 2 is =query('data from Airtable'!A:L",select *") which tells the function to select all columns from the data.
Recommended read: Google Spreadsheet Multi Select Dropdown
You can also use date/time literals in your query, such as DATE, TIMEOFDAY, TIMESTAMP, or DATETIME. These literals must be in a specific format, such as yyyy-MM-dd or HH:mm:ss[.SSS].
In summary, the query syntax in Google Sheets is made up of three main parts: the data range, the query, and the headers. The query is written using the Google Visualization API Query Language, and the headers are optional.
If this caught your attention, see: Clear All Headers of Anything Googledocs
All
Using a named range in Google Sheets can be a game-changer for your QUERY function. It makes the function much easier and cleaner to use.
To create a named range, select your data range and go to the menu: Data > Named ranges... A new pane will show on the right side of your spreadsheet.
You can refer to your table of data easily by entering a name in the first input box. This name can be anything you like, but it's helpful to choose something descriptive.
The named range "countries" already exists in the template, so feel free to use it. Remember, our QUERY function is in cell G1, so the output will be in columns G, H, I, etc.
Broaden your view: Google Spreadsheet Range Function
Query Conditions
The Google Sheets Query function allows you to filter data based on specific conditions.
You can use the WHERE clause to specify a condition that must be satisfied. For example, to select only countries with a population greater than 100 million, you can use the WHERE keyword with a condition like "population > 100 million".
To combine multiple conditions, you can use logical operators like AND, OR, and NOT. For instance, to select data where the value in column I is more than or equals 40 and the string in column E does not include the Denver sandwich, you can use the formula "select C, E, I WHERE I>=40 and not E='Denver sandwich'".
Here are some examples of using logical operators in the WHERE clause:
- AND: "select C, E, I WHERE I>=40 and E='Denver sandwich'"
- OR: "select C, E, I WHERE I>=40 or E='Denver sandwich'"
- NOT: "select C, E, I WHERE not I>=40"
You can also use the IN operator to select data where a value matches one of several specified values. For example, to select data where the value in column E is either "Denver sandwich" or "New York pizza", you can use the formula "select C, E, I WHERE E IN ('Denver sandwich', 'New York pizza')".
Worth a look: Transfer Google Fi Esim to New Phone
Here are some basic operators you can use in the WHERE clause:
- =: equal to
- <>: not equal to
- >=: greater than or equal to
- <=: less than or equal to
- >: greater than
- <: less than
- IS NULL: checks if a cell is empty
- IS NOT NULL: checks if a cell is not empty
You can also use advanced operators like STARTS WITH, ENDS WITH, CONTAINS, MATCHES, and LIKE to perform more complex queries. For example, to select data where the string in column E starts with the letter "C" and the string in column C starts with the letter "K", you can use the formula "select C, E, I WHERE E STARTS WITH 'C' and C LIKE 'K%'".
Here are some examples of using advanced operators in the WHERE clause:
- STARTS WITH: "select C, E, I WHERE E STARTS WITH 'C'"
- ENDS WITH: "select C, E, I WHERE E ENDS WITH 'C'"
- CONTAINS: "select C, E, I WHERE E CONTAINS 'C'"
- MATCHES: "select C, E, I WHERE E MATCHES 'C'"
- LIKE: "select C, E, I WHERE E LIKE 'C%'"
Sorting and Limiting
Sorting and Limiting in Google Sheets Query is a breeze. You can sort data in ascending or descending order using the ORDER BY clause.
To sort in ascending order, you need to use the string "order by A" with the column ID, as shown in Example 1. This will pull all data and order it by the order ID in ascending order.
You might enjoy: Google Slide Id
You can also sort data by date using the ORDER BY clause followed by date, as demonstrated in Example 3. This will sort the entire dataset based on the values in the specified date column in ascending order.
To limit the number of rows pulled from another sheet, you can use the LIMIT clause, as shown in Example 4. This will pull all data and limit the returned result to the first 5 rows + the header.
Here are some examples of ORDER BY clauses:
- "select * order by A" – sorts in ascending order
- "select * order by A DESC" – sorts in descending order
- "ORDER BY B" – sorts by date in ascending order
Sort in Ascending
To sort data in ascending order using Google Sheets Query, you need to use the ORDER BY clause followed by the column ID you want to sort by. This is crucial to avoid including empty cells in your output.
When sorting data, it's essential to add "is not null" to your string to exclude empty cells. You can see this in Example 1, where the string "select * where A is not null order by A" pulls all data and orders it by order ID (column A) in ascending order.
For another approach, see: Google Spreadsheet String to Number

The syntax for sorting in ascending order is straightforward: "select * order by A". This will pull all data and order it by the values in column A.
To sort data by date in ascending order, use the same ORDER BY clause followed by the date column ID, like in Example 3: "ORDER BY B". This will sort the entire dataset based on the values in column B (date of order) in ascending order.
Here's a summary of the syntax for sorting in ascending order:
- 'data from Airtable'!A:L – the data range to query on
- "select * where A is not null order by A" – the string to sort data by column A in ascending order
6: Limit
Limiting your data can be a game-changer when working with large datasets. The LIMIT clause in Google Sheets Query reduces the number of rows pulled from another sheet.
You can use the LIMIT clause in combination with a specific formula, like this: "select * Limit 5". This string pulls all data and limits the returned result to the first 5 rows, including the header.
If you want to skip a certain number of rows and then limit the result, you can use the LIMIT and OFFSET clauses together. The syntax is "select * Limit 5 Offset 10", which pulls all data, skips the first 10 rows, and limits the result to 5 rows excluding the header.
Suggestion: Google Fi Data Limit
Here's a quick reference to get you started:
- “select * Limit 5" pulls all data and limits the returned result to the first 5 rows + the header.
- "select * Limit 5 Offset 10" pulls all data, skips the first 10 rows, and limits the result to 5 rows excluding the header.
Remember, the LIMIT clause comes in handy when you need to work with a specific subset of your data.
Observe Results
You'll immediately see the results in the cell where you wrote the QUERY function.
The data displayed will match the criteria and format defined in your query.
Make sure you don't have any data in the columns where you are typing your QUERY function and expecting new data to go.
The information from columns B and D was copied into the columns where the QUERY function was written.
Explore further: How to Form Table from Query in Google Sheet
Pivoting and Grouping
You can use the PIVOT clause in Google Sheets Query to create your own pivot tables, as seen in example #4. The PIVOT clause essentially transposes data from one column to a row with new columns, grouping other values accordingly.
If rows of the pivot columns contain the same values, the PIVOT clause will aggregate them. So, if you don’t use GROUP BY as part of the PIVOT clause, you will get a table with one row only, as demonstrated in the Google Sheets Query PIVOT without GROUP BY example.
On a similar theme: How to Lock Row in Google Spreadsheet
You can use the GROUP BY command in the PIVOT clause to get the desired result. For instance, if you want to return a PIVOT table with the names of burgers in the heading row and the list of customers as the main column, you can use the string "select C, sum(G) Group BY C Pivot E".
Every column used in the select clause should be covered with an aggregate function, or mentioned in the group by command following the pivot. This ensures that the data is properly aggregated and grouped.
Here's a summary of the syntax for using the PIVOT clause with GROUP BY:
Data Manipulation
Data manipulation with Google Sheets Query is a powerful tool for extracting insights from your data. The Google Visualization API query language has three core functions and operators that help you manipulate your data: arithmetic operators, aggregation functions, and scalar functions.
To start manipulating your data, you'll need to select it. Begin by typing the equal sign (=) followed by the QUERY function in the cell where you want the query results to appear. Inside the function, you'll provide the necessary arguments: the data range, the query itself, and the number of header rows (if applicable).
These core functions and operators will help you extract the insights you need from your data.
For another approach, see: Google Spreadsheet Custom Function
Data Manipulation
Data manipulation is a powerful tool in Google Sheets, allowing you to extract and transform data to suit your needs. You can use the Google Visualization API query language to manipulate your data, which specifies three core functions and operators: arithmetic operators, aggregation functions, and scalar functions.
Arithmetic operators are used to perform mathematical operations on your data, such as addition and subtraction. These operators can be used to calculate new values based on existing data.
Aggregation functions, on the other hand, are used to group and summarize data. For example, you can use the SUM function to add up a range of numbers.
Scalar functions are used to perform a single operation on a value, such as converting a date to a specific format. You can use the TEXT function to convert a date to the format "yyyy-mm-dd".
To reference cells in your query, you can use concatenating operators and a text function. This allows you to reference values from other cells, such as dates or text strings.
Here are the three core functions and operators in the Google Visualization API query language:
- Arithmetic operators: +, -, \*, /
- Aggregation functions: SUM, AVERAGE, COUNT
- Scalar functions: TEXT, DATE
By mastering these functions and operators, you can unlock the full potential of data manipulation in Google Sheets.
Arithmetic and Aggregation
Arithmetic and Aggregation functions are powerful tools in Google Sheets Query that allow you to manipulate your data with ease. You can use arithmetic operators like +, -, /, and * to perform basic calculations.
Arithmetic operators include + (plus), - (minus), / (divide), and * (multiply). For example, in the query "select C, I, G*H label G*H 'Arithmetic multiplication'", the value in column G is multiplied by the number in column H.
Aggregation functions, on the other hand, apply to one column and execute an operation across all rows of that column. They include avg(), sum(), count(), max(), and min(). These functions can be used in the SELECT, ORDER BY, LABEL, and FORMAT clauses.
Some examples of aggregation functions include avg() to provide the average of all numbers in a column, and sum() to provide the sum of all numbers in a column. You can also use count() to provide the quantity of items in a column, max() to provide the maximum value of all in a column, and min() to provide the minimum value of all in a column.
Intriguing read: Google Photo Select All
Here's a breakdown of the aggregation functions:
You can use these functions in your queries to extract the desired information from your data. For example, in the query "select min(B), count (C), max(I), avg(G), sum(I)", the minimum value from column B, the count of items in column C, the maximum value from column I, the average of column G, and the sum of column I are all extracted.
If this caught your attention, see: How to Create G Mail
Formatting and Labeling
Formatting and Labeling is a crucial aspect of Google Sheets Query. You can use the LABEL clause to assign a name to a heading field of one or multiple columns.
The LABEL clause can be used in combination with other clauses, and it's optional. You can use column IDs or the results of arithmetic operators, scalar, or aggregation functions as elements in this clause. For example, you can use the string "select * label C 'customer', E 'Burger', I 'Total paid'" to pull all data and give columns C, E, and I new labels.
Consider reading: Google Sheet Split Text to Columns
You can also use the LABEL clause to rename columns in a query. For instance, in the example =QUERY(Papers!A1:G11",select A,B,C label A 'Name', B 'Date'"), the column A is renamed to 'Name' and the column B is renamed to 'Date'.
If you need to rename multiple columns, you can separate each new pair of column-label by a comma. The format clause allows you to alter the format of all values in a column. You can use a pattern to specify the desired format. For example, =QUERY(Papers!A1:G11",select A,B,C format B 'mm-dd, yyyy, ddd'") formats the date in column B as 'mm-dd, yyyy, ddd'.
Worth a look: Google Sheet Get Sheet Using Name
9: Format
The format clause is a powerful tool in Google Sheets QUERY that allows you to alter the format of all values in a column.
You can use a pattern to specify the desired format, and the format clause can even be used on its own in a Google Sheets QUERY.

To use the format clause, you'll need to specify the column you want to format and the pattern for the desired format.
For example, if you want to format a date column in the format 'dd-mmm-yyyy', you can use the format clause like this: format B 'dd-mmm-yyyy'.
You can also use the format clause to change the label of a column, like this: label J 'Hour'.
Here are some examples of date formats you can use in the format clause:
- 'dd-mmm-yyyy'
- 'mm-dd, yyyy, ddd'
You can find more date formats by going to Format > Number > More Formats > Custom number format in Google Sheets.
A fresh viewpoint: Google Spreadsheet Format as Table
Label
The Label clause in Google Sheets QUERY allows you to assign a name to a heading field of one or multiple columns.
You can use column IDs or the results of arithmetic operators, scalar, or aggregation functions as elements in this clause. The LABEL clause is optional, and you can put it first in the query string.

To use the LABEL clause, follow this format: "select * label C 'customer', E 'Burger', I 'Total paid'". This string pulls all data and gives columns C, E, and I new labels.
Here are some examples of how to use the LABEL clause:
- "select A,B,C label A 'Name', B 'Date'" - This query renames columns A and B.
- "select C, sum(D) group by C order by sum(D) label sum(D) 'Courses Taken'" - This query renames the sum of column D to 'Courses Taken'.
The LABEL clause is useful for making your data more readable and understandable. By assigning meaningful names to your columns, you can make it easier to analyze and understand your data.
Advanced Techniques
You can add a total row to your Query formulas by using the "select" clause with the "sum" function. This will give you a summary row at the bottom of your data.
The Query language used in Google Sheets QUERY function is a text-based language similar to SQL, and it uses clauses to perform actions. You can use various clauses to perform a wide variety of actions on your data.
If you want to summarize your data by year, you can use the YEAR scalar function. This function will roll up your data by year, making it easier to analyze.
Recommended read: Google Spreadsheet Row
The Query function also allows you to use advanced comparison operators, such as "starts with", "ends with", "contains", "matches", and "like", to run more complex queries.
Here are some examples of how to use these operators:
You can use these operators to filter your data in creative ways, such as pulling data from columns C, E, I, where the value in column E (product) starts with the “C” letter and where the string in column C (customer name) starts with the “K” letter.
Tips and Tricks
If you're new to Google Sheets queries, it's easy to get overwhelmed by the options. Start with the basics and you'll be querying like a pro in no time.
To select all columns, use the asterisk (*) symbol, like this: SELECT *. It's a simple way to get a quick view of your data.
When you need to focus on specific columns, list their headers separated by commas. For example, SELECT A, C, E. This is great for isolating the information you need.
On a similar theme: Nexus S
Filtering rows is a crucial part of querying. Use the WHERE clause to narrow down your data based on specific conditions. For instance, WHERE B > 100. This helps you find exactly what you're looking for.
To perform calculations on your data, utilize aggregate functions like SUM, COUNT, and AVERAGE. These functions are essential for getting insights from your data.
Here are some common aggregate functions and what they do:
Operators and Clauses
Google Sheets Query operators and clauses are used to set conditions and perform actions on your data. They are similar to SQL and help you filter, sort, and manipulate your data with ease.
You can use simple comparison operators like =, <>, >, >=, <, and <= for numeric values. For strings, you can use complex comparison operators like contains, starts with, ends with, matches, and != (doesn't match / doesn't equal to). Logical operators like and, or, and not can be used to combine several conditions.
Here's a list of possible clauses you can use in Google Sheets Query:
- WHERE clause: used to set conditions towards the data you want to get
- SELECT clause: used to specify the columns you want to retrieve
- FROM clause: used to specify the data range to query on
- GROUP BY clause: used to group data by one or more columns
- HAVING clause: used to filter grouped data
Arithmetic Operators (+)
Arithmetic Operators (+) are a fundamental part of Google Sheets Query, allowing you to perform basic calculations. They include + (plus), - (minus), / (divide), and * (multiply).
These operators can be used to execute calculations on data from your data source, such as multiplying values in columns G and H. The result of the calculation is a number, which can be returned as part of your query.
You can use arithmetic operators to pull data from specific columns and perform calculations on the fly. For example, you can select columns C and I, and multiply the value in column G by the number in column H.
Arithmetic operators can be used in combination with other clauses to create complex queries. They can also be used with aggregation functions like sum, average, count, max, and min to perform calculations on groups of data.

The syntax for using arithmetic operators is straightforward. For example, "select C, I, G*H label G*H 'Arithmetic multiplication'" multiplies the value in column G by the number in column H and changes the label of the column with multiplication to 'Arithmetic multiplication'.
Here are some examples of arithmetic operators in action:
- 'data from Airtable'!A:L – the data range to query on
- "select C, I, G*H label G*H 'Arithmetic multiplication'" – the string pulls the data from columns C, I, multiplies the value in column G by the number in column H, and changes the label of the column with multiplication to ‘Arithmetic multiplication’.
- Note, that the pulled from the data source sheet value in column B equals the calculated result shown in column C.
Clauses Used in
The Google Sheets QUERY function uses clauses to perform various actions on your data. Clauses are used to set conditions, filter data, and manipulate data.
You can use several clauses within one Google Sheets QUERY function, and they need to follow a specific order. The order of clauses is crucial, so make sure to follow it.
The WHERE clause is used to set conditions towards the data you want to get. It acts as a filter, searching columns for values that meet your conditions and fetching all matches back to you. You can use simple comparison operators like =, <>, >, >=, <, <= for numeric values, and complex comparison operators like contains, starts with, ends with, matches, != (doesn't match / doesn't equal to), like for strings.
On a similar theme: How to Set Your Google Chrome Default Email 2024
You can also use logical operators to combine several conditions, such as and, or, not. And you can use operators for blank / not empty, like is null, is not null.
Here's a list of logical operators you can use in the Google Sheets QUERY function:
- and
- or
- not
These operators can be used within the WHERE clause to set complex conditions. For example, you can use the and operator to specify that a value must meet two conditions, or the or operator to specify that a value must meet one of two conditions.
The SELECT clause is used to specify which columns you want to retrieve from your data. You can use scalar functions, like the ones mentioned in Example 5, to perform calculations on your data.
The Google Sheets QUERY function uses the Google API Query Language, which is similar to SQL. You can use various clauses, like WHERE, SELECT, GROUP BY, PIVOT, ORDER BY, LABEL, and FORMAT, to manipulate your data.
A fresh viewpoint: Google Spreadsheet Not Equal Operator
Multiple Queries
You can query multiple columns by specifying their IDs, like 'data from Airtable'!A:L where you select C, E, I columns. This is done by defining the data range to query on and then specifying the columns you want to pull data from.
To query different sheets in Google Sheets, you can use an array formula enclosed in curly brackets, listing the sheets you want to pull data from, separated by semicolons. For example, {'data from Airtable'!A1:L; Sheet1!A1:L; Sheet2!A1:L}.
If you want to query some data from another spreadsheet, consider using a combination of QUERY and IMPORTRANGE functions. This allows you to pull data from multiple sheets and sources, making it a powerful tool for data analysis and manipulation.
A unique perspective: How to Pull Data from One Google Sheet to Another
One or Multiple
You can query one or multiple columns in Google Sheets using the QUERY function. To fetch only a certain column, define it by a column ID, like 'data from Airtable'!C.
To pull multiple columns, separate their IDs with commas, as in "select C, E, I". This will return all data from the specified columns.
You can also use an array formula enclosed in curly brackets to query multiple sheets. For example, {'data from Airtable'!A1:L; Sheet1!A1:L; Sheet2!A1:L} includes the list of sheets to pull data from, separated by semicolons.
If you want to query data from another spreadsheet, use a combination of QUERY and IMPORTRANGE.
15: Across Tabs
You can use the QUERY function to select data from multiple tabs in a spreadsheet.
To do this, you'll need to enclose the data ranges in curly braces {} and separate them with a semicolon. The format changes a bit, referring to columns by their number (Col1, Col2, etc.) instead of their letter (column A, B, etc.).
For example, =QUERY({Sheet4!A1:F7;Sheet3!A1:F9}, “Select Col1”) will return the data from column 1 in both Sheet4 and Sheet3.
Note that the data in the sheets should be in the same format, so in this example, the ages should be in the second column in both sheets.
If you want to query data from another spreadsheet, consider using a combination of QUERY and IMPORTRANGE.
Here's a simplified example of how to query multiple sheets:
- Array formula: {'data from Airtable'!A1:L; Sheet1!A1:L; Sheet2!A1:L}
- Select all data where Col1 is not null: "select * where Col1 is not null"
Frequently Asked Questions
What is the difference between VLOOKUP and query in Google Sheets?
VLOOKUP is ideal for simple lookups, while QUERY is better suited for complex data manipulations like sorting, filtering, and summarizing. If you need to perform multiple operations on your data, QUERY is the way to go
What is the difference between Importrange and query?
Importrange imports data from another spreadsheet, while Query searches specific information within that imported data, allowing you to filter and extract what you need
Can I write a SQL query in Google Sheets?
Yes, you can write SQL-like queries in Google Sheets using the QUERY option, giving you the power of SQL without a database. Learn how to harness this feature and unlock new data retrieval possibilities!
What is the purpose of a query in Google Sheets?
A query in Google Sheets helps you extract and manipulate specific data from a spreadsheet with ease, allowing you to filter, sort, and group data as needed. With a query, you can quickly uncover insights and patterns in your data.
Featured Images: pexels.com


