How to Query From Another Sheet using Query function
To query data from another sheet using the Query function in Google Sheets, you should use the IMPORTRANGE function within the Query function to import the data from the other sheet. Here's a step-by-step guide on how to do this:
- First, you need the URL of the Google Sheet that contains the data you want to query. If the data is in the same Google Sheet but on a different tab, you can skip this step.
- Use the IMPORTRANGE function to import the data from the other sheet. The syntax for the IMPORTRANGE function is:
IMPORTRANGE("spreadsheet_url", "sheet_name!cell_range")
- Replace "spreadsheet_url" with the URL of the Google Sheet that contains the data you want to query (or leave it empty if the data is in the same Google Sheet).
- Replace "sheet_name" with the name of the sheet where the data is located.
- Replace "cell_range" with the range of cells that you want to query.
- Use the Query function to query the data from the other sheet. The syntax for the Query function is:
QUERY(data, query, [headers])
- Replace "data" with the IMPORTRANGE function from Step 2.
- Replace "query" with the SQL-like query that you want to perform on the data.
- Replace "[headers]" with the number of header rows in the data (optional).
Example
Let's assume you have two sheets in the same Google Sheets document:
- Sheet1, which contains the data you want to query
- Sheet2, where you want to display the query results
The data in Sheet1 is as follows:
Name | Age | Gender |
---|---|---|
Alice | 27 | F |
Bob | 32 | M |
Carol | 29 | F |
Dave | 24 | M |
In Sheet2, you want to display the names and ages of all females. To do this, use the following formula:
=QUERY(IMPORTRANGE("", "Sheet1!A1:C4"), "SELECT Col1, Col2 WHERE Col3 = 'F'")
This will display the following results in Sheet2:
Name | Age |
---|---|
Alice | 27 |
Carol | 29 |
Note: If the data is in another Google Sheet, replace "" in the IMPORTRANGE function with the URL of that Google Sheet.
Did you find this useful?