Google Sheets Query Another Workbook? How to Link Data

How-to basics · Updated November 23, 2026

Yes, you can query another workbook in Google Sheets, but not directly with the QUERY function alone. The standard approach is to first import the external data into your current sheet using IMPORTRANGE, then wrap that in QUERY. For example, =QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID", "Sheet1!A1:D100"), "SELECT * WHERE Col2 > 10") pulls data from another spreadsheet and filters it in one step.

Why You Can’t QUERY Another Workbook Directly

Google Sheets’ QUERY function only works on data that already exists within the same spreadsheet file. It cannot reach across to another file on its own. This is a deliberate design choice for security and performance—allowing direct cross-file queries would require every spreadsheet to expose its data to any other file that references it, creating privacy risks and potential slowdowns. Instead, Google provides IMPORTRANGE as the bridge: it imports a range from another spreadsheet into your current one, and then QUERY can process that imported data as if it were local.

How to Combine IMPORTRANGE and QUERY

The combined formula is straightforward, but you need to know the spreadsheet ID and the exact range. Here’s the step-by-step:

  1. Open the source spreadsheet (the one you want to pull from). Copy the URL from the address bar. The ID is the long string between /d/ and /edit.
  2. In your target sheet, type =QUERY(IMPORTRANGE("SPREADSHEET_ID", "Sheet1!A1:D100"), "SELECT * WHERE Col1 IS NOT NULL"). Replace SPREADSHEET_ID with the actual ID, and adjust the sheet name and range.
  3. Press Enter. The first time you use IMPORTRANGE on a source you haven’t accessed before, Google Sheets will prompt you to allow access. Click Allow access. Without this, the formula returns a #REF! error.

Note that the range in IMPORTRANGE must be in quotes and include the sheet name (or just the range if you want the first sheet). Also, in the QUERY clause, columns are referenced as Col1, Col2, etc., not letters.

Practical Example: Pulling Sales Data from a Separate File

Suppose you have a file named “Sales2024” with a sheet called “Orders” containing columns: Order ID (A), Date (B), Amount (C), Region (D). You want to pull only orders from the “West” region with amounts over $500. The formula would be:

=QUERY(IMPORTRANGE("1AbC123XyZ...", "Orders!A1:D1000"), "SELECT * WHERE Col4 = 'West' AND Col3 > 500")

This returns all columns for matching rows. If you only need specific columns, change the SELECT clause, e.g., SELECT Col1, Col3.

Common Errors and How to Fix Them

Several issues can arise when using this combination:

  • #REF! error: Usually means you haven’t granted access. Click the cell, then the “Allow access” button that appears, or re-enter the formula and click the prompt.
  • #VALUE! error: Often due to a syntax mistake in the QUERY clause, like using single quotes incorrectly or referencing a column that doesn’t exist.
  • #N/A error: The range you specified might be empty or the sheet name is wrong. Double-check the sheet name and range.
  • Data not updating: IMPORTRANGE refreshes periodically, usually within a few minutes. If you need immediate refresh, you can use a workaround like re-entering the formula or using a script, but for most cases the delay is acceptable.

Performance Considerations and Alternatives

Using IMPORTRANGE inside QUERY can slow down your spreadsheet if you import huge ranges. The entire range is pulled into your sheet, even if QUERY only needs a few rows. To improve performance, limit the range in IMPORTRANGE to the exact rows/columns you need. For example, instead of A1:Z1000, use A1:D500 if you know the data won’t exceed that.

If you need to query multiple workbooks, you can nest multiple IMPORTRANGE functions, but that gets messy. A better alternative is to use the Google Sheets API or Apps Script to pull data programmatically, but that requires coding. For most users, the IMPORTRANGE+QUERY combo is sufficient.

How This Differs from Excel

In Microsoft Excel, you can query an external workbook directly using Power Query (Get & Transform Data). You don’t need a separate import function; you can connect to another Excel file and query it with M language. However, in Google Sheets, IMPORTRANGE is the only built-in way to bring data from another spreadsheet. If you’re migrating from Excel, this is a key difference to remember.

Frequently Asked Questions

Can I use QUERY on an IMPORTRANGE that pulls from a different tab in the same spreadsheet?

No, IMPORTRANGE is only for other spreadsheets. To query another tab in the same file, you can reference it directly, like =QUERY(Sheet2!A1:D100, "SELECT *").

Why does my QUERY(IMPORTRANGE) show a #REF! error even after I allow access?

Sometimes the permission prompt doesn’t appear if the source spreadsheet is shared with you but not with the current file’s owner. Ensure you have at least view access to the source, and try re-entering the formula. If the error persists, check that the spreadsheet ID is correct.

Is there a limit to how much data I can import with IMPORTRANGE?

Yes, Google Sheets has limits on the number of cells you can import per formula and per spreadsheet. Typically, IMPORTRANGE can pull up to 50,000 cells per formula, and there are overall spreadsheet limits. If you exceed these, you’ll get an error.

Can I use QUERY(IMPORTRANGE) to join data from two different workbooks?

Yes, you can use multiple IMPORTRANGE functions and combine them with QUERY’s JOIN or by using array literals. For example, {IMPORTRANGE("id1", "Sheet1!A1:B10"), IMPORTRANGE("id2", "Sheet1!A1:B10")} creates an array, but you’ll need to handle column alignment carefully.

How often does IMPORTRANGE update?

Google Sheets refreshes IMPORTRANGE data periodically, typically every few minutes. There’s no manual refresh button, but you can force a refresh by making a small edit to the formula or using a script trigger.

Security and Sharing Permissions

When you use IMPORTRANGE, you are granting the target spreadsheet access to the source spreadsheet’s data. The source file must be shared with the email address that owns the target spreadsheet (or with anyone who has edit access, depending on settings). If you share the target spreadsheet with others, they will also see the imported data, but they won’t be able to edit the source unless they have permission. Always be cautious about importing sensitive data—ensure the source is shared only with trusted collaborators.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.