In this article:

How to SQL Query Multiple Sheets in Google Sheets

>Click here to Import a MYSQL Database to a Lido Spreadsheet

It would be quite a hassle if we have to combine sheets upon sheets of data into a single sheet just to run queries to it, right? Fortunately, it is possible to query multiple sheets in a single line in Google Sheets. This tutorial will teach us a simple trick to do so. Before we start, take note of this advice:

Make sure that these sheets contain the same types of columns so we can take advantage of this trick. 

Are you ready? Let’s go!

If you want to include two or more sheets of the same workbook

Again, let us go back to the basic format of a QUERY command in Google Sheets:

=QUERY(source_sheet, "sql_query_commands", headers)

To combine two or more sheets as the source, list the sheet ranges, separated by semicolons without spaces, enclosed in curly braces {}. For our example, we want to combine week27 and week28:

=QUERY({week27!A1:H633;week28!A2:H634})

Running this command alone will combine the two sheets together, which is another trick in combining the contents of sheets without the manual copy-paste method. Google Sheets will automatically insert more rows at the bottom when needed, but it takes some time.

end of data displayed by query. There are 1267 rows loaded by the QUERY command.
Here, there are 1267 rows loaded by the QUERY command.

If you want to include two or more sheets from different Google Sheets workbooks

If the sheets to be queried are located in a different Google Sheets workbook, you will need to use the IMPORTRANGE function. It has the following function:

=importrange("url_of_workbook","data_range")

Note that both the URL and the data range should be enclosed by quotation marks. You can insert this function inside a query function, so the formula will look like this:

=QUERY( {importrange("url_of_sheet1","data_range");importrange("url_of_sheet2","data_range");importrange("url_of_sheet3","data_range")}, "sql_query_commands")

The workbook must be either a public workbook or that you have access to it. If the workbook you are including is also yours, a prompt will pop up, asking you to connect the sheets. Click Allow access. 

 A pop-up prompt by Google Sheets to allow access to another Google Sheets worksheet. Statement: You need to connect these sheets.


Afterward, the data will be loaded. 

If the sheets are located in the same workbook (thus having the same URL), they still need to be added through the IMPORTRANGE function individually. 

If you also want to apply queries to two or more sheets

How about inserting queries? Queries will still work, with a little change in how we call the columns. Instead of referring to the column letters, we will use Col1, Col2, Col3, and so on. Let’s say we want to list the date, total sales, and profit. These are located in columns 1, 5, and 8, so we will call on Col1, Col5, and Col8:

=QUERY({week27!A1:H633;week28!A1:H634}, "select Col1, Col5, Col8")

The result will look like this:

Same thing but selected columns loaded: date of entry, total price of order, and profit from the order.
Pretty neat, right?


If you think this is quite a hassle, consider this instead

While this lets us analyze large chunks of data much easier, it will take us a lot of time to do so. Maybe there is a better way so that all the analysis is automated and we will just have to make important decisions for our business...

If that’s what you fancy, consider trying Lido. With a few clicks, you can now access all the relevant metrics without going through the hassle of accessing the SQL databases of your eCommerce platforms and then coding the formulas to process them. Let our platform do it all for you!


Schedule a free automation consult
Learn more

Automate repetitive tasks with Lido

Save hours on repetitive and tedious work. Lido is a new spreadsheet that connects your spreadsheets, forms, PDFs, and email inbox.

Trigger
Action
Select trigger
When a cell value in Google Sheets changes
Check Google Sheets for today's date
When a new row is added to Google Sheets
On new Google Form submission
Send me a daily reminder
On new Typeform submission
When a cell value in database changes
Check database for today's date
When a new row is added in database
When a new HubSpot customer is created
Select action
ADDCALENDAREVENT
Creates a one- or multiple-day calendar event with optional attendees
ADDHUBSPOT
Adds an object to Hubspot
ADDSLACKCHANNEL
Create a Slack channel, and optionally add a topic or members
CALLURL
Makes any HTTP request
CREATEGOOGLEDOC
Replaces [@column_name] values in a Google Doc with the corresponding table row's values
CREATEPDF
Replaces [@column_name] values in a Google Doc with the corresponding table row's values, then export it as a PDF
FETCH
Makes any HTTP request and returns the response
INSERTROWS
Inserts given array below defined values in given worksheet
SENDGMAIL
Sends an email using your Google account
SENDOUTLOOK
Sends an email using your Microsoft Outlook account
SENDSLACK
Sends a Slack message
SENDSMS
Sends an SMS message using your Twilio account
UPDATECELL
Updates cells with given values
UPDATEHUBSPOT
Updates a property of a Hubspot object
Trigger is required
Action is required
Get started