Get help

Help center

Guides and answers

Contact support

Write to our team

Sign in to

Workflows

Build processes with your team

Automations

Connect files and automate tasks

One system, four parts

AI agents

Virtual Employees that work 24/7

Automations

Connect files and automate tasks

Workflows

Build processes with your team

Data Space

Trusted data sharing and control

Solutions by department

Finance

Reports, budgets, and reconciliations

Inventory and manufacturing

Stock, orders, and production in one view

HR

Timesheets, attendance, and reviews

Project management

Status and workload across projects

Sales and CRM

Leads and deals kept up to date

Education

Student records and school data

Finance

Turn manual finance work into reports that update themselves.

What Finance teams automate

  • Track payables and receivables
  • Build cash flow statements
  • Compare budget against actuals

Inventory and manufacturing

See stock levels and automate ordering.

What inventory and manufacturing teams automate

  • Track inventory across multiple sites
  • Connect sales, purchasing, and warehouse data
  • Link a barcode scanner to your sheet

HR

Run recruitment, attendance, and OKR tracking from your sheets.

What HR teams automate

  • Track recruitment and onboarding
  • Record team OKRs and results
  • Track check-ins, check-outs, and leave

Project management

Assign tasks and track hours without manual updates.

What project teams automate

  • Assign tasks and track status changes
  • Estimate effort and log hours worked
  • Track milestones across projects

Sales and CRM

Track leads and sales performance with automated reports.

What sales teams automate

  • Track and convert leads
  • Set sales goals and track team metrics
  • Monitor sales KPIs on dashboards

Education

Automate attendance, grades, and school purchasing.

What schools automate

  • Track attendance and notify parents
  • Record and compare class grades
  • Track inventory lent to students and staff

Find a use case

By department

How each team uses Sheetgo

By industry

How each industry uses Sheetgo

By company size

Small, medium-sized, or large

Learn

Blog

Guides and product news

Customer stories

How teams like yours use Sheetgo

Help center

Guides and answers

Why Sheetgo

Compare

How Sheetgo compares with other tools

Security

How we protect your data

Trust center

Our compliance details

Sign in to

Workflows

Build processes with your team

Automations

Connect files and automate tasks

One system, four parts

AI agents

Virtual Employees that work 24/7

Automations

Connect files and automate tasks

Workflows

Build processes with your team

Data Space

Trusted data sharing and control

Solutions by department

Finance

Reports, budgets, and reconciliations

Inventory and manufacturing

Stock, orders, and production in one view

HR

Timesheets, attendance, and reviews

Project management

Status and workload across projects

Sales and CRM

Leads and deals kept up to date

Education

Student records and school data

Find a use case

By department

How each team uses Sheetgo

By industry

How each industry uses Sheetgo

By company size

Small, medium-sized, or large

Get started

Templates

Ready-made workflows to start from

Learn

Blog

Guides and product news

Customer stories

How teams like yours use Sheetgo

Help center

Guides and answers

Why Sheetgo

Compare

How Sheetgo compares with other tools

Security

How we protect your data

Trust center

Our compliance details

Help center

Guides and answers

Contact support

Write to our team

Workflows

Build processes with your team

Automations

Connect files and automate tasks

Combine QUERY with IMPORTRANGE in Google Sheets

Combining QUERY with IMPORTRANGE allows you to pull in data from different spreadsheets and perform advanced queries on it. Whether you’re managing project progress, financial data, or team efforts, mastering these functions can enhance your productivity and data management.

IMPORTRANGE is one of the most useful Google Sheets functions. As the name suggests, it imports a range of cells from one spreadsheet to another.

On the other hand, QUERY is an incredibly versatile formula, essentially allowing you to use your spreadsheet as a database. With SQL-like query language, you can look up, sum, count, average, filter and sort data.

Below you’ll find the syntax, five examples, and a step-by-step setup in two real files, including how to allow access and how to group and sort the imported data.

How to use QUERY with IMPORTRANGE in Google Sheets

This combination applies QUERY directly to the data transferred with IMPORTRANGE to filter specific columns, rows, and ranges.

It’s a real time-saver when working with multiple spreadsheets, as you only pull in the exact data you need. It also allows you to format the data within your query instead of afterwards, which saves you an additional step.

Syntax

=QUERY(IMPORTRANGE(spreadsheet_url, range_string), query, [headers])
  • spreadsheet_url – the link to the spreadsheet where data is imported from.
  • range_string – range of cells to be imported. This can be further broken down into two components: 
    • sheet_name – the name of the sheet with the desired data 
    • cell_range – the range of the cells within this sheet
  • query – criteria of which data to transfer. Query handles language functions (Select, Group by, etc.) and data manipulation functions (Avg, Sum, Count, and Multiply).

How does this combination work?

Whenever you combine functions, things can get a bit confusing. To avoid that, let’s look at what’s going on in this syntax.

First, let’s focus on QUERY. This function can manipulate data in many ways, but always needs to start with a data source.

In many cases this is data from another tab on the same spreadsheet. But as you know, this article deals with importing data from another spreadsheet. As a result, we will use the IMPORTRANGE function to feed data into QUERY.

IMPORTRANGE is a simple function: just input the link to the spreadsheet and the range you want to import. The first time you connect two files, it shows a #REF! error until you allow access. The step-by-step setup further down shows where to click.

This imported data can be plugged into the QUERY function. After all you need to input are the query parameters, which dictate how the data will be manipulated and displayed. The examples below illustrate this.

Just remember, the structure of the syntax remains similar, no matter which function you nest inside Google Sheets’ QUERY function.

Example 1: Import and Query

In the following examples, I’ll use this geographical data as the source data:

QUERY IMPORTRANGE source data — country list with region and last data date in Google Sheets

First, I import country data from 186 countries into a business spreadsheet. I’ve merged items from a separate dataset based on certain criteria.

Say I only want to import the data from countries in the Americas. To display only those specific cells, you simply need to apply the Select and Where clauses inside the QUERY function.

=QUERY(IMPORTRANGE("Spreadsheet_url", "Population!B1:F187"), "Select * Where Col1='Americas' ")
QUERY IMPORTRANGE example — Select * Where Col1 equals Americas returns only countries in the Americas

In this formula, QUERY searches data from a predefined range/array according to specific criteria. Simultaneously, IMPORTRANGE imports the data into the target spreadsheet.

Placing QUERY before IMPORTRANGE allows you to search (query) specific information from the data that is imported with IMPORTRANGE. This way, you only import the data you want to see in your spreadsheet.

After IMPORTRANGE, you need to add search criteria. To import data from countries in the Americas, we used this query: Select * Where Col1=’Americas’.  In plain English that translates to “select all the data that has “Americas” in column 1”.

This formula comes in especially  handy in situations where you want to merge data. If you work in online marketing for example, you might want traffic from various sources into one spreadsheet. It gives you a better overview of several data sources such as Google Analytics, Ahrefs, SEMrush or Google Search Console.

Example 2: Import data within a date range

In this next example, I added search criteria after IMPORTRANGE. In this case, where Col3 > date ‘2019-07-01’ and Col3 < date ‘2019-12-30’ imports the same data but only for items in the list that were published between those dates.

Note that when you input a date into your query, you need to type the word date before it. Also don’t forget to put the date in quotation marks. That’s how the function knows you’re referring to a date.

=QUERY(IMPORTRANGE("Spreadsheet_url", "Population!A1:F187"), "where Col3 > date '2019-07-01' and Col3 < date '2019-12-30'", 0)

You might apply this formula in logistics or even for a small business cash register. You can add up rows, columns and data ranges with changes in the data over time. If you’re interested in changes to product lines, sales, and other KPIs over the course of the year, this formula can carry out monthly or quarterly comparative analysis.

QUERY IMPORTRANGE date range — Col3 between two dates in 2019 filters the imported rows

Example 3: Import and combine multiple spreadsheets with specific criteria

If you want to extract certain cells or a range from several spreadsheets, use the following formula. Here’s how to merge data from multiple spreadsheets into one:

=QUERY({IMPORTRANGE("Spreadsheet_url","Sheet1!A1:P");IMPORTRANGE("Spreadsheet_url","Sheet1!A2:P");IMPORTRANGE("Spreadsheet_url","Sheet1!A2:P")},"Select * where Col1<>"",1)
QUERY with multiple IMPORTRANGE — three spreadsheets stacked in curly brackets into one table

You need to use IMPORTRANGE for every spreadsheet you want to merge. Within the first IMPORTRANGE formula, I added the headers in A1:P.

The other spreadsheets also contain the headers. However, they should not be imported multiple times. To avoid importing the header three times, I’ve changed the range to A2:P on the second and third sheets.

Lastly, the Where clause (where Col1<>’’) simply states that all cells that are not blank should be imported.

Here are a few tips for importing multiple sheets:

  • The list of imports needs to be enclosed in curly brackets and a semicolon must separate each IMPORTRANGE.
  • The imported sheets must all have the same layout. For example, if the first column of all the sheets wasn’t Country Name, the data wouldn’t combine properly.
  • Don’t import the headers on every sheet – you only need one.
  • Remember to allow access to all the spreadsheets you want to import from.

This formula is really useful for business surveys (when you collect several spreadsheets with the same format). Likewise for educational purposes. When you hand out similar exercises to students or when you collect grades from various professors.

Example 4: Import and Query Average Values

With this formula, I import the average population of each continent. As you can see, I add “avg” after the ‘Select’ clause and I put the column in parentheses. Next, add the query (the data you are looking for) after the Where clause. In this case, the query is ‘Europe’.

Notice how on the rest of this spreadsheet I use multiple QUERY/IMPORTRANGE functions to get average populations and total GDPs for different regions. Since QUERY is highly customizable, you can get all sorts of different outputs depending on your objective!

=QUERY(IMPORTRANGE("Spreadsheet_url", "Population!A2:P187"), "Select avg(Col4) where Col2 contains 'Europe' ")

QUERY function allows you to extract all arithmetic functions. This is a very helpful tool for traders and financial analysts. With it, you can look up values from a variety of investment spreadsheets and carry out calculations such as Present Value =PV, Future Value =FV and interest rate functions such as =IRR and =RATE.

In addition: it’s a more intuitive, natural language version of the complex INDEX-MATCH formulas in Excel.

QUERY IMPORTRANGE average — avg(Col4) returns the average population for Europe

Example 5: Import and Query Sum values

In this final example, the formula filters by continent, sums up each country’s GDP and imports data from a separate spreadsheet.

This is specifically helpful to create dashboards and reports of all kinds.

=QUERY(IMPORTRANGE("Spreadsheet_url"), "Select sum(Col5) where Col2 contains 'Europe' ")
QUERY IMPORTRANGE sum — sum(Col5) returns the GDP total for Europe

How to set up QUERY with IMPORTRANGE between two files

The examples above use a placeholder for the spreadsheet URL. Here is the whole setup in a real pair of files, with the URL and the range in their own cells so the formula stays short.

The example below is based on two files. The source file contains data on sales for all agents and products. The destination file is where I want to import and analyze data based on specific conditions.

Copy the source URL and cell range

1. Open both the source and destination files in Sheets.

QUERY IMPORTRANGE in Google Sheets — SALES_DATA source file with sales_id, sales_amount, sales_agent, product, and sales_team columns

2. From the source file, copy the URL.

IMPORTRANGE source URL — the SALES_DATA URL being copied from the browser address bar

3. Paste the URL into the destination file.

IMPORTRANGE source URL — SALES_ANALYSIS file with the source URL pasted in B1

4. Go back to the source file and select the cell range. You can copy the cell range reference from the top-left corner, as shown below.

IMPORTRANGE cell range — range A1:E505 copied from the name box of the source file

5. Paste the cell range reference into the destination file. I don’t need to specify the sheet because it is on “Sheet1”.

IMPORTRANGE cell range — A1:E505 pasted in B2 of the destination file

Add the QUERY function

I’ll use a simple query with ‘select’ and ‘where’ clauses to import the rows where the “sales_team” is the ‘Southteam’.

1. Start typing the formula for QUERY.

QUERY IMPORTRANGE formula — =QUERY( typed in A4 with the data, query, and headers hint

2. For the ‘data’ argument, add IMPORTRANGE and reference the cells that hold its arguments, B1 and B2.

QUERY IMPORTRANGE formula — IMPORTRANGE(B1,B2) added as the data argument of QUERY

3. For the ‘query’ argument, I will use “Select * Where Col5=’Southteam'”. ‘Col5’ is the column “sales_team”, which should match the “Southteam”.

QUERY IMPORTRANGE formula — Select * Where Col5 equals Southteam added as the query argument

4. Press “Enter” to see the results.

QUERY IMPORTRANGE results — imported rows where sales_team is Southteam

5. You can easily add another condition by using the ‘OR’ operator. For instance, I can import the results for two different teams by adjusting the query as shown below.

QUERY IMPORTRANGE with OR — query extended to Southteam OR Westteam in Col5

6. As you can see in the screenshot below, the results now also contain the data for ‘Westteam’.

QUERY IMPORTRANGE results — sales_team filter shows Southteam and Westteam rows

Allow access between the files

If you’re working with files that have never been connected before, you’ll see an error message instead of the value when you press ‘Enter’. To see the actual value, you will have to approve access between sheets. Click the cell with the #REF! error, then click Allow access. You only need to do this once for each pair of files.

IMPORTRANGE allow access — #REF! error with the You need to connect these sheets prompt and Allow access button

How to group and sort imported data with QUERY

For the sake of simplicity, I’ll use the data from the previous section. For my analysis, I want to see sales totals by team, as well as sales totals by sales agent.

QUERY IMPORTRANGE example — a new formula started in A4 with IMPORTRANGE(B1,B2)

Group by sales team

To group the results, I need to select the ‘sales_team’ column (5) and the sum of the ‘sales_amount’ column, then group by ‘sales_team’.

The query looks like this: “Select Col5,sum(Col2) Group by Col5”.

QUERY IMPORTRANGE group by — Select Col5, sum(Col2) Group by Col5 typed in A4

Press ‘Enter’ to see the results.

QUERY IMPORTRANGE group by — sales totals for Eastteam, Northteam, Southteam, and Westteam

Group by sales agent

As the name of the sales agent is in column 3, I need to modify the query and replace references to ‘Col5’ with ‘Col3’.

QUERY IMPORTRANGE group by — Group by Col3 formula in D4 to total sales per agent

Press ‘Enter’ to see the results.

QUERY IMPORTRANGE group by — sum of sales_amount per sales agent, sorted by name

Order by sales amount

As you can see in the previous screenshot, the function automatically sorts the column alphabetically by the name of the sales agent. However, I want to see the results by sales amount, in descending order.

I can modify the previous query by adding “Order by sum(Col2) DESC” at the end.

QUERY IMPORTRANGE order by — Order by sum(Col2) DESC added to the group by query

Press ‘Enter’ to see your results ordered by sales amount.

QUERY IMPORTRANGE order by — sales agents ranked by total sales, highest first

Get the same results with Sheetgo connections

Sheetgo offers an intuitive and powerful alternative to the combination of QUERY and IMPORTRANGE in Google Sheets. With Sheetgo, you can import and process data automatically, so your spreadsheet data is ready for reports, dashboards, and AI.

Coincidentally, one of the key data processing capabilities of Sheetgo offers QUERY filter. This feature enables you to perform advanced data queries similar to the QUERY function in Google Sheets. You can filter, sort, and manipulate data as it flows between your spreadsheets, all without the need for complex formulas.

Why use Sheetgo over IMPORTRANGE? Here are just three reasons:

  • Enhanced Data Management: Unlike IMPORTRANGE, Sheetgo provides a centralized dashboard where you can manage all your data connections.
  • Scalability: Sheetgo can handle large datasets and multiple connections without the performance issues that IMPORTRANGE might encounter. This makes it ideal for businesses with complex data requirements.
  • Advanced Filtering: With Sheetgo’s QUERY filter, you can perform sophisticated data manipulations directly within your workflows. This allows for more advanced data processing capabilities compared to IMPORTRANGE.

Find out all the reasons why Sheetgo is the best IMPORTRANGE alternative.

Conclusion

Now you’ve got the lowdown on how to use QUERY with IMPORTRANGE, from allowing access between files to grouping and sorting the imported data. Now you can merge the power of these two functions, for another great combination check out How to use VLOOKUP and IMPORTRANGE.

If your dataset is too large for QUERY+IMPORTRANGE to handle, the no-formula IMPORTRANGE alternative shows how to filter and connect Google Sheets at scale without crashing.

You may also like…

google sheets features and formulas

How to find nominal interest rate using Google Sheets

Sarah is a young professional making significant financial decisions. From credit cards to mortgages, student loans, and investment...
google sheets features and formulas

Pull Google Calendar data into Google Sheets using Apps Script

This post was originally published in our community forum. Objective : This guide demonstrates how to use Google Apps Script to pull data...
google sheets features and formulas

Using the PROPER function to capitalize the first letter of each word in Google Sheets

Note: This post was originally published in our community forum. Ever found yourself needing to clean up a list of names, titles, or any...