How to use the QUERY function in Google Sheets
If you need to manipulate data in Google Sheets, the QUERY function can help you! It brings powerful database type search capabilities to spreadsheets, so you can look up and filter data in any format you want. The following article will show you how to use the QUERY function in Google Sheets.
Use the QUERY function
The QUERY function is not too difficult to master if you have ever interacted with the database using SQL. The format of a typical QUERY function is similar to SQL and brings the power of database search functionality to Google Sheets.
The format of a formula using the QUERY function is:
=QUERY(data, query, headers)
Replace 'data' with a range of cells (for example, 'A2: D12' or 'A: D' ).
The optional 'headers' argument sets the number of header rows to include at the beginning of the data range. If you have a title that spans two cells, like First in A1 and Name in A2 , then QUERY will use the content of the first two rows as the combined heading.
In the example below, a worksheet (called the 'Staff List' ) of a Google Sheets spreadsheet includes a staff list. It contains the name, employee ID number, date of birth, and whether they attended mandatory employee training.

On the second worksheet, you can use the QUERY formula to get a list of all employees not attending the required training. This list will include the employee ID number, first name, last name, and whether they attended the training.
To do this with the data shown above, you can enter:
=QUERY('Staff List'!A2:E12, "SELECT A, B, C, E WHERE E = 'No'")
This queries data from the range A2 to E12 on the 'Staff List' page.
Like a regular SQL query, the QUERY function selects the columns to display ( SELECT ) and specifies the parameters for the search ( WHERE ). It returns columns A, B, C and E listing all matching rows, where the value in column E ( 'Attended Training' ) is a text string that says 'No'.

As shown above, the 4 staff members on the list did not attend the training. The QUERY function provided this information, as well as matching columns to display employee names and ID numbers in a separate list.
This example uses a very specific data range. You can change to query all data in column A to E. This will allow you to continue adding new employees to the list. The QUERY formula you used will also update automatically whenever you add new employees or when someone joins a training session.
The exact formula to do this is:
=QUERY('Staff List'!A2:E, "Select A, B, C, E WHERE E = 'No'")
This formula ignores the original heading 'Employees' in cell A1.
If you add the 11th employee, not attending the training to the original list, as shown below ( Christine Smith ), the QUERY formula will also update and show the new employee.

Advanced QUERY formula
The QUERY function is very flexible. It allows you to use other logical operations (such as AND and OR ) or Google functions (such as COUNT ) as part of the search. You can also use comparison operators (bigger, smaller, etc.) to find values between 2 numbers.
Use comparison operators with QUERY
You can use QUERY with comparison operators (such as smaller, bigger or equal) to narrow and filter data. To do this, the article will add a column ( F ) to the 'Staff List' table , with the number of awards each employee has won.
Using QUERY, we can search all employees who have won at least one award. The format for this formula is:
=QUERY('Staff List'!A2:F12, "SELECT A, B, C, D, E, F WHERE F > 0")
This uses a comparison operator > to search for a value greater than 0 in column F .

The above example shows the QUERY function returns a list of 8 employees who have won one or more awards. Out of 11 employees, 3 have never won an award.
Use AND and OR with QUERY
Nested logical operators like AND and OR work well in larger QUERY formulas, to add more search criteria to the formula.
A good way to test AND is to look up data between two dates. If applied to the employee list example, the article can list all employees born between 1980 and 1989. This also takes advantage of comparison operators, such as greater than or equal to (> = ) and less than or equal to (<=).
The format for this formula is:
=QUERY('Staff List'!A2:E12, "SELECT A, B, C, D, E WHERE D >= DATE '1980-1-1' and D <= DATE '1989-12-31'")
This formula also uses the additional DATE function to accurately analyze the timestamp and search all birth dates between January 1, 1980 and December 31, 1989.

As stated above, 3 employees born in 1980, 1986 and 1983 meet these requirements.
You can also use OR to generate similar results. If using the same data, but converting dates and using OR , for example, all employees born in the 1980s can be excluded.
The format for this formula would be:
=QUERY('Staff List'!A2:E12, "SELECT A, B, C, D, E WHERE D >= DATE '1989-12-31' or D <= DATE '1980-1-1'")

Of the original 10 employees, 3 were born in the 1980s. The above example shows the remaining 7 people who were born before or after the excluded days.
Use COUNT with QUERY
Instead of simply searching and returning data, you can also mix QUERY with other functions, like COUNT, to manipulate the data. Let's say, for example, we want to remove some employees from the list of people who have participated in the required training course.
To do this, you can combine QUERY with COUNT as follows:
=QUERY('Staff List'!A2:E12, "SELECT E, COUNT(E) group by E")

Looking at column E ( 'Attended Training' ), QUERY function used COUNT to count the number of times each value type (containing the text string Yes or No ). From the example list, 6 employees have completed the training and 4 have not.
You can easily change this formula and use it with other types of Google functions, like the SUM function.
You should read it
- 30+ useful Google Sheets functions
- 9 basic Google Sheets functions you should know
- How to create custom functions in Google Sheets
- The multiple-choice question set has an answer to Query P1
- How to count on multiple sheets of Google Sheets
- How to count words on Google Sheets
- How to use the FLOOR function in Google Sheets
- How to generate random numbers in Google Sheets
May be interested
- How to generate random numbers in Google Sheetsgoogle sheets provides a simple function for generating random numbers in spreadsheets without leaving the document or installing add-ons. this function returns a random integer between two values. and this is how to use it.
- Keep track of the stock market with Google Sheetsnot only does it help with calculating tasks, making statistics lists, google sheets also owns a feature that few people know, which is to monitor the stock value over time, constantly updated.
- How to create graphs, charts in Google Sheetsgoogle sheets also features a graphical representation, showing the data that are included so that users can change the chart.
- How to use Filter function on Google Sheetsfilter functionality on google sheets will help users find the data they need in the data sheet, based on the conditions we use.
- How to align spreadsheets before printing on Google Sheetsbefore printing spreadsheet data on google sheets, users should adjust the data sheet before printing to get a better layout.
- How to count on multiple sheets of Google Sheetsto sum a cell or region on multiple sheets on google sheets, users will still use the sum function.
- How to use the AVERAGE function in Google Sheetsaverage function in google sheets to calculate the average value between arguments.
- How to enter 0 in Google Sheetsnormally when entering 0 on google sheets, it will automatically disappear if that 0 is in front of the number line. so how to re-display 0 in google sheets.
- How to create a phone number can be called on Google Sheetsgoogle sheets has a call feature when entering phone numbers into data tables, via callers installed on the computer.
- How to view editing history on Google Sheetsgoogle sheets will automatically save the content edits on the file so that users can review it when needed, or restore the modified version.