Importing data from one sheet to another using IMPORTRANGE in Google Sheets is a task that many of us have performed. This article will cover using query and importrange in Google Sheets, but did you know there’s a whole world of possibilities beyond just importing?
With IMPORTRANGE and QUERY functions, you can do much more than that. These functions enable you to transfer all data between Google Sheets, allowing you to manipulate data and gain greater control over what information is nested and displayed in the target spreadsheet. Additionally, you can combine QUERY and IMPORTRANGE to pull data from multiple spreadsheets and perform advanced queries, making them essential tools for effective data management and analysis.
In this article, we’ll explain how to use them, show examples, discuss their benefits, and provide a template that you can use in your work.
The QUERY function in Google Sheets enables you to ask questions and obtain specific answers from your data. This function requires a data source to manipulate information. Let’s explore how it works.
=QUERY(data_range,"query_string")
Here is the breakdown:
Using QUERY can help avoid the need for complex formulas.
Example:
Let’s say we have a list of students’ names in column B and their scores in column C. To find out who scored higher than 90, you can use the following QUERY formula.
=QUERY(B3:C12, "SELECT B, C WHERE C > 90")
This instructs the function to search cells B3 to C12, identify the names and scores where the score is greater than 90, and display them in the result. So, you’d get a list of students’ names and scores, but only for those who scored 90 or higher.
The IMPORTRANGE function in Google Sheets helps you bring in data from another sheet or even a different Google Sheets file using the importrange formula.
=IMPORTRANGE("spreadsheet_url","range_string")
Let’s break it down:
Example:
Let’s use our student table from the previous example. Suppose we have a sheet called “Student List” in our Google Sheets file. To import the faculty information for each student from the sheet into our current sheet, we will use the IMPORTRANGE formula, selecting the appropriate imported data range.
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!D3:D12")
This function will import faculty information for each student from the specified range in the “Student List” sheet into our current sheet.
What if we need to combine data from multiple spreadsheets in Google Sheets and then analyze it in a specific way?
Here’s how it works:
=QUERY(IMPORTRANGE("spreadsheet_url", "data_range"), "query_string")
Continuing with our example, let's say we have one sheet with student names and scores, and another with their faculties.
By combining QUERY and IMPORTRANGE, you could find out the faculties of students who scored above 90. For more details, including how to apply multiple criteria, you can refer to the documentation.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:D"), "SELECT Col1, Col3 WHERE Col2 > 90", 1)
With QUERY and IMPORTRANGE in Google Sheets, you can do a lot. You can import specific data, combine data from different sheets, remove unnecessary rows, search for specific text, and more. Handling several spreadsheets is particularly useful when combining and analyzing data, especially in scenarios like business surveys and educational assessments, where similar formats are present across these spreadsheets.
To facilitate this process, you can use the following formula to import and combine data from multiple spreadsheets while adhering to certain criteria.
Mastering these techniques makes your work more efficient. And what’s more, all these cases are available for you in our template.
If you would like to learn more about finding data with specific criteria in Google Sheets, you can read our guide on VLOOKUP functions.
Now, you’re ready to explore the versatility of QUERY and IMPORTRANGE functions further. So let’s dive into each use case and uncover the full potential of combining QUERY with IMPORTRANGE to optimize your data management tasks and Google Sheets experience.
The most basic scenario involves importing a specific data range from another table. Utilising various data sources, including Google Analytics and SEMrush, can provide a comprehensive overview for online marketing purposes. For instance, we have a Google Sheets file with student information, but only want to import the scores and faculties of the students (range B3:C12) into another sheet.
Here's how you can combine IMPORTRANGE with the SELECT clause in QUERY to import specific columns from the "Student List" sheet, and then choose exactly which columns you want to pull.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:D"), "SELECT Col1, Col2, Col3")
If you have data scattered across different sheets and want to merge it into 1, specifying a source file for each dataset is crucial. QUERY and IMPORTRANGE can help streamline this process. There are two different methods of merging your data.
For merging horizontally, separate IMPORTRANGE functions by commas:
=QUERY({IMPORTRANGE("spreadsheet1_url", "data_range"), IMPORTRANGE("spreadsheet2_url", "data_range"),..}, "query_string")
For merging vertically, separate IMPORTRANGE functions by semicolons:
=QUERY({IMPORTRANGE("spreadsheet1_url"; "data_range"), IMPORTRANGE("spreadsheet2_url"; "data_range"),..}, "query_string")
Remember to import separately from each sheet before using QUERY and IMPORTRANGE, especially when applying the where clause to avoid errors. Specify the source, such as 'Orders from Airtable', to ensure accurate data import.
We have scores in another sheet named “Scores” and faculties in named “Faculty”. To merge this information into 1 sheet using QUERY and IMPORTRANGE, use the formula below.
Formula:
=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/1oad79FkDOx2iGdeiJDEcKeBcVZGDHDl9MDiK0WAa13g/edit?usp=sharing", "Scores!B2:C12"), IMPORTRANGE("https://docs.google.com/spreadsheets/d/1oad79FkDOx2iGdeiJDEcKeBcVZGDHDl9MDiK0WAa13g/edit?usp=sharing", "Faculty!B2:C12")}, "SELECT Col1, Col2, Col4")
Note that when you import data using IMPORTRANGE from a specific spreadsheet for the first time, the formula prompts you to grant access. Simply click on the small button that appears in the cell to allow access.
You can use the “limit” and “offset” QUERY clauses to control the number of rows imported and remove unnecessary ones.
The “limit” clause sets the maximum number of rows to import, excluding the header. For instance, “limit 5” would import only 5 rows of data.
Meanwhile, the “offset” clause determines the number of rows to skip from the top of the imported data. For example, “offset 2” would skip the first 2 rows of data.
So, if you use “limit 5” and “offset 2” together, you’ll import 5 rows of data starting from the third row. This helps streamline your data analysis by focusing only on the relevant information.
Efficiently managing numerical data, such as total price, is crucial during data manipulation processes. This ensures that you can apply conditions to focus on products with a total price exceeding a certain amount when transferring data from Airtable.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:D"), "SELECT Col1, Col2, Col3 LIMIT 5 OFFSET 2")
You can customize the imported column names using the “label“ QUERY clause.
The “label” clause allows you to rename the headers of the imported columns. For instance, we can change “Score” to “Grade”.
Additionally, you can use the SELECT SUM function to filter and aggregate data, such as calculating the total GDP for specified regions, which is useful for generating informative dashboards and reports.
To achieve this task:
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:D"), "SELECT Col1, Col2, Col3 LIMIT 5 LABEL Col2 'Grade'")
This formula imports the first 5 rows of data from columns B, C, and D of the "Student List" sheet and renames the header accordingly.
In case you need to apply custom formatting to the values in imported columns, you can adjust the formatting of imported columns using the “format” QUERY clause. Advanced users often utilize the query importrange in Google Sheets to manipulate imported datasets effectively, showcasing their skills and enhancing their understanding of these functions.
For instance, you can format dates in the "E" column to display only the month and year.
For this:
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing","Student List!B:E"), "SELECT Col1,Col2,Col3,Col4 FORMAT Col4 'mmm-yyyy'")
With the QUERY clause in Google Sheets, you can selectively filter rows within imported columns by applying specific conditions using the WHERE statement. By combining functions like QUERY and IMPORTRANGE, you can pull specific data from various sources into one spreadsheet, enabling better organization and analysis of information across multiple sheets.
Let's say, we need to apply a filter to the imported data, specifying that only rows where the value in column C is greater than or equal to 80 should be included.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing","Student List!B:E"), "SELECT Col1,Col2,Col3,Col4 WHERE Col2>=80")
When using the WHERE statement with QUERY and IMPORTRANGE, you're not limited to just 1 rule, and can actually add a few rules using AND or OR.
So, let's change our previous formula a bit. Along with getting students who scored 80 or more in column C, let's also get students whose names start with the letter "J".
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing","Student List!B:E"), "SELECT Col1,Col2,Col3,Col4 WHERE Col1 STARTS WITH 'J' AND Col2>=80")
We can specify criteria for what a particular column should contain to meet our needs. This can be done using the WHERE clause with a contains operator.
Let's use the formula below to filter our student data only to retrieve those in the "Mathematics" faculty.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing","Student List!B:E"), "SELECT Col1,Col2,Col3,Col4 WHERE Col3 CONTAINS 'Mathematics'")
When we're using the ORDER BY clause with QUERY, we can sort our data based on a specific column, either in ascending or descending order.
In this case, we want to:
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing","Student List!B:E"), "SELECT Col1,Col2,Col3,Col4 WHERE Col2>=80 ORDER BY Col4 DESC")
We can perform basic arithmetic operations like add (+), subtract (-), multiply (*), and divide (/) columns imported from a spreadsheet and display the result as a separate column.
Our task is to:
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:E"), "SELECT D, SUM(C) WHERE D <> '' GROUP BY D LABEL D 'Faculty', SUM(C) 'Total Scores'")
In Google Sheets, you can use aggregation functions within select, order by, label, and format QUERY clauses to perform calculations on imported columns:
Our task:
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:E"), "SELECT D, SUM(C) WHERE D <> '' GROUP BY D LABEL D 'Faculty', SUM(C) 'Total Scores'")
When using scalar functions, we can transform imported data into a single value.
Consider the table with columns B to E containing student names, scores, faculty, and date of birth. If you apply the year() function to the date of birth values, it will extract the year. So, if you have dates like 26/01/1994, year() will return 1994.
Formula:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1W9jTjw3XH0mI1rLVeHXej_vqn1L7dVC1djisb4gJtW4/edit?usp=sharing", "Student List!B:E"), "SELECT Col1, Col2, Col3, YEAR(Col4) LABEL YEAR(Col4) 'Year of Birth'")
When using QUERY and IMPORTRANGE in Google Sheets, you may encounter some common issues. Here are a few tips to help you troubleshoot and avoid potential errors:
By keeping these tips in mind, you can effectively troubleshoot and resolve common issues when using the QUERY and IMPORTRANGE formula in Google Sheets. For more detailed solutions, you can refer to our articles on IMPORTRANGE and QUERY error solutions.
If you want to improve your Google Sheets skills, learn more about mastering more advanced functions like ARRAY, XLOOKUP, and UNIQUE:
OWOX: Reports, Charts & Pivots Extension offers a more straightforward way to work with data, even if you're not a spreadsheet expert. With its intuitive interface, you can work with complex data and produce high-quality reports. Whether you're analyzing sales, customer behavior, or marketing data, the OWOX Reports Extension for Google Sheets helps you extract useful data without the need to master complex spreadsheet techniques.
Yes, you can use IMPORTRANGE with QUERY in Google Sheets to import data from another spreadsheet and then apply the QUERY function to filter, sort, and manipulate the imported data.
To use the IMPORTRANGE function in Google Sheets, you have to include the URL of the source spreadsheet and the range of cells to import. For example: =IMPORTRANGE("source_spreadsheet_url", "sheet_name!range"). You must also grant access to the source spreadsheet for the function to work.
To import data into a QUERY in Google Sheets, first use the IMPORTRANGE function to bring data from another spreadsheet. Then, wrap the IMPORTRANGE function with the QUERY function. Use the following formula: =QUERY(IMPORTRANGE("spreadsheet_url", "data_range"), "query_string")
IMPORTRANGE is used to import data from one Google Sheets spreadsheet to another, while IMPORTDATA is used to import data from a given URL, such as a CSV or TSV file hosted on the web. IMPORTRANGE requires permission from the source spreadsheet, while IMPORTDATA does not.