To query from another tab within the same spreadsheet in Google Sheets, you can use the following syntax:
=query(stats!A1:C9, “select A, B“, 1)
This returns columns A and B from the cell range A1:C9 within the tab named stats. The 1 specifies that there is 1 header row at the top of the dataset being queried.
To query from another spreadsheet entirely, you can use the following syntax:
=query(importrange(“URL“, “stats!A1:C9“), “select Col1, Col2“, 1)
This returns the first two columns from the cell range A1:C9 within the tab named stats within the Google Sheets spreadsheet with a specific URL.
The following examples show how to use these functions in practice.
Example: Query from Tab in Same Spreadsheet
Suppose we have the following Google Sheets spreadsheet with two tabs:
- stats
- new_sheet
To perform a query on the data in the stats tab and return the results of the query in the new_sheet tab, we can type the following formula in cell A1 of the new_sheet tab:
Example: Query from Another Spreadsheet
Now suppose that we would like to query from another spreadsheet entirely. To do so, we simply need to identify the URL of the Google Sheets spreadsheet that we’d like to query from.
For example, suppose the data we’re interested in is located at the following URL:
We can use the importrange() function to query data from this spreadsheet:
Notice the subtle difference between this example and the previous example:
- When querying from a tab within the same spreadsheet, we use select A, B
- When querying from an entirely different spreadsheet, we use select Col1, Col2
You can find more Google Sheets tutorials on this page.