If you’re already familiar with basic Sheets querying, and looking to master it – I’d recommend heading straight to the CIFL Querying course. It includes detailed walkthroughs and quizzes that you won’t find here.

Why we Query

QUERY combines all of the capabilities of arithmetic functions (SUM, COUNT, AVERAGE) with the filtering abilities of a function like FILTER.

In this example, we’ll walk through a few QUERY examples, to analyze a sampling of Twitter data.

The basic structure of a QUERY is to take a data range and a query string:

=query( ‘data tab’!A:C, “select A where C = ‘twitter’”)

Let’s answer a few questions, all using QUERY functions:

Q: What was the average number of retweets from the sample?

A: =query(D1:G, “select avg(G)”)

Q: What was the average number of retweets on Tweets that contained an @mention but weren’t a retweet?

Date comparisons in Google Sheets queries: why so complex?

When I first started writing date comparisons within Google queries (pulling data before / after a certain date), it was a pain to figure out the correct syntax.

Unfortunately it’s not really mentioned anywhere in the Google Sheets documentation, so I had to figure it out for myself.

You have to do two things correctly:

Write the string ‘date’ before the actual date, to declare your intentions

Type your date in the format ‘yyyy-mm-dd’

If you want to compare a date dynamically from another cell, it’s a bit of a different story. I like to keep date ranges (last 7 days, last 30 days, year-to-date, etc), then reference them in a query like this:

Notice the syntax:

To combine the string with an outside value, close the query double-quotes, and use ‘&’

Use the TEXT function to convert the date into the correct ‘yyyy-mm-dd’ format

Once you get the hang of it, you’ll never think about it again. But if you do ever forget the syntax, the CIFL cheat sheet has your back.

Debugging common Query errors

Errors are never fun, but Sheets’ error messages will always point you in the right direction:

#REF

1) Array result was not expanded because it would overwrite data in D6

Wrap your dropdown value in single quotes (”), if it’s a string (none if it’s a number)

Close (and then re-open) your query with double quotes (“”)

Use ampersands (&) to add the dropdown value to the query string

Reference the actual cell of the dropdown value (‘data’!A2) to add it to the query string

That’s it! Your report users will thank you.

Google Sheets QUERY vs SQL: what’s the difference?

If you’re used to writing SQL queries, it can be off-putting at first to work with Google Sheets queries (and vice-versa).

If you don’t know any SQL yet, learn Query first. It’ll make working with databases *much* easier when you get there.

Let’s walk through a number of ways in which they’re similar & different, and how you can adapt each of them in your work.

Basic querying

SQL: SELECT * FROM table WHERE column_name = ‘xyz’ ORDER BY column_name desc LIMIT 10Sheets: =query( ‘tab’!A:D, “SELECT * WHERE A = ‘xyz’ ORDER BY A desc LIMIT 10”)

The basic query syntax is roughly the same. Google Sheets queries use the same SELECT statement to choose columns, WHERE / AND / OR to set logic, ORDER BY to arrange results, and LIMIT to pull only a certain number of results (see a full list of language clauses in the Google docs).

The key difference is that there’s no ‘FROM’ statement in a Sheets query – instead of referencing a table, you reference a range of cells (‘tab’!A:D in this example).

Also, you’ll notice that, instead of referencing column names (like in SQL), in Sheets queries you reference columns by their letter (A, B, C, etc) or by their order (Col1, Col2, Col3).

Doing math on columns

SQL: SELECT column_name, sum(other_column_name) FROM table GROUP BY column_nameSheets:=query( ‘tab’!A:D, “SELECT A, sum(B) GROUP BY A”)

The same avg(), count(), sum(), max(), min() aggregations functions that you’re used to using in SQL are available in Sheets queries, as are basic +, -, *, / arithmetic functions.

Date comparisons

Dates in Google queries are tricky to figure out the first time, if you’re used to working in SQL.

Unlike SQL, which can handle multiple date formats (‘20160826’, ‘8/26/2016’, ‘2016-08-26’), Sheets can only take dates in format ‘yyyy-mm-dd’ – so you sometimes have to use the TEXT function to transform a date like 8/26/2016 into 2016-08-26.

In Sheets, you also have to specify that you’re comparing a date, by adding the phrase ‘date’ before your date string.

Fuzzy phrase matching

SQL: SELECT column_name FROM table WHERE column_name LIKE “%phrase%”Sheets:=query( ‘tab’!A:D, “SELECT A WHERE A CONTAINS ‘value’ “)

To pull text that contains a value, you can simply use ‘contains,’ versus the ‘like’ statement in SQL. Sheets queries do have a ‘like’ statement, but I find myself using ‘contains’ much more frequently because of its simplicity.

One added wrinkle though, if you’re looking to add a ‘not contains’ parameter, you have to place the ‘not’ before the column letter.

This takes all of the values that are present in Tab 2, column C, and looks for them in column B of Tab 1.

It’s a poor man’s nesting query, but it works :).

Phew!

That was a lot of Google Sheets querying, but I hope it’s starting to sink in for you.

Make sure to grab a free copy of the Sheets formula cheat sheet below – but if you’ve made it this far, you’re probably ready to master Sheets by investing a few hours in the advanced CIFL Querying course (which includes detailed walkthroughs and quizzes not found here).

One idea – you could nest 3 importrange functions together – place them within { }, and separate the three function calls with ; – ie { importrange1(first 15k rows) ; 2nd 15k ; 3rd 15k }. That’ll achieve the same result of returning ~45000 rows.

Hi there. I’ve been using gsheets for some months and faced how powerful it is. Anyway, surfing around I found your blog and just loved your comparisions and methods.
I’m one of these guys who like to unlock all resources of a tool before deep into codes.
I have a suggestion about join tables with query formula. Despite google doesnt support joining tables with query there is actually a way to do a subquery. It is not the same but can help with lots of troubles

That’s an interesting approach Allan, thanks for sharing – joining in Sheets queries to me has been the holy grail, the cup of gold of Sheets functions. Your idea doesn’t **exactly** work as you scoped it – you’d have to concatenate a long string of ‘OR A contains X or A contains Y or A contains Z’ – rather than referencing the entire range X:Z in one shot.

If I can make it work, I’ll share a video about it – thanks for the inspiration.

Great work on these videos, which have been a great intro to this amazing function which I’m still marvelling at the power of. One tip I’ve picked up, and I’m not sure if it is a more recent function introduced to Google Sheets than this webpage, is that textjoin(” OR B = “, TRUE, tab2!C2:C”) is, I think, a slightly more elegant solution to the concatenate “blah” nested query.

Yes, this is the alternative implementation of “IN” described a few months ago (in a comment below). You’d still want to combine this with a “Unique()” filter, for tidiness. The advantage of what you have here is that it deals with the blanks. I was not aware of that extra option in the TEXTJOIN function vs the plain JOIN function. I’m surprised their Help pages don’t link to each other.

However the clause is generated, this really doesn’t create a ‘join’ query, though. It won’t align the rows from table A with those of table B based on the matching criteria. (If correctly constructed) it will simply return the rows in the first table which happen to match SOME prescribed values in the second. You can’t pull in any extra columns from table B. In that case, it simply degenerates into the equivalent of an “IN” clause, which is already addressed more directly.

I am having a problem with some of my QUERY’s wherein the data from the sheet (same book) that I am QUERYING changes based on inputs from a FORM. In other words, there are changes all of the time happening.
Here is my failing query that does not refresh unless I delete the equals sign in the formula and then CTRL-Z that change:
=query({STAGINGsuREPORT!A3:L},SQL(“STAGINGsuREPORT!A3:L”,CONTROL_SU!E39,true),true)

STAGINGsuREPORT is made up of THIS….
=query({PULLMASTEReditLIMITED!A1:R},SQL(“PULLMASTEReditLIMITED!A1:R”,CONTROL_SU!E9,true),true)
and THIS…
=ARRAYFORMULA(IF(ROW(E3:E)=3,{“Agent”,”Owner”,”Contact No”,”Property Address”,”SPECIAL UNDERTAKINGS”,”SupportLinks”,”EDITdots”},IFERROR(VLOOKUP(A3:A,STIPSsorted!B2:Z,{3,4,5,6,9,19,20}*SIGN(ROW(A3:A)),FALSE),{“”,””,””,””,”No SU, Provisions or Stipulations entered”,””,””})))

BUT STAGINGsuREPORTS as a sheet always refreshes without issue.

…Parts of the above:
…SQL is a Google Script that allows use of Column Names versus Col1 format. (I like this because colomn names can change.)
…and CONTROL_SU is the sheet where I hold my SELECT contents.

I saw some suggestions that updating / refreshes are a problem that used to be handled with a NOW() portion in the formula, but that does not work now.

Hey Jim, my hunch is that this is due to using a Google Script (SQL) within the query – they often won’t refresh based on an automated edit to the Sheet (like a form response). I’d first try removing that dependency as a test, and see if that does the trick.

Well…. I thought it worked!
But it fails again!
Here is the formula that DOES NOT use the SQL script:
=query({COLORS3!A3:BC},SQL(“COLORS3!A3:AZ”,”select TRACT,PARID,shortNAME,LINE,COUNTY,ABOVE_THE_LINE,APPROACH_TYPE,AGENT,MAP_NOTES,FOLDER_,NOTES_,NEW_,MAP,SU,REPORT,AGENTLIST,TAP,OL_TAP,TAP_DONE,CLOSED,OPENtracking,ROWworkONLY,LOD,PROXIMITY,DAMAGE,NOTICE,EASE,WS,SA,AR,DRAW_CONFLICT,WSreductionSU,WSremovalSU,TC_LEGAL,PO_GROUP,POhasREP,CONDEMNATION,CPG_OWNED,PO_STATUS where TRACT != “””” ORDER BY TRACT ASC LABEL ABOVE_THE_LINE “”ATL”””, true),true)

And here is the one that uses the SQL script:
=query({COLORS3!A3:BC},SQL(“COLORS3!A3:AZ”,CONTROL_DS!E22,true),true)

The ERROR I get pretty consistently is that TRACT is not … a field? (I can’t remember the exact error, but it complains like it cannot get TRACT from the COLORS3 sheet…. which does exist and is updated. I just think the updates are not synchronized enough for the second sheet to realize that the first sheet was being updated and is now fully populated.

I am putting a lot of info into this comment. Please forgive me for messing up your Comments section. Please help as you can?

Thanx!
PS. Sometimes the previous sheet appears populated, but I do notice that the calling cell says “Loading…” and holding the mouse over the cell shows me a dialog with: “Error Loading Data…”

Essentially, you’re pinging an API each time you use the QUERY function. This API has a limit – you can’t actually tell what that limit is (there’s no way to know how close you are to the limit until you’ve passed it), but when you pass it, your calculations will start to lag out (and eventually will fail/return nothing). You’re then pretty severely rate limited until your quota refills (I’m not sure how long that takes, but I’d say somewhere in the several hours/12 hours range).

The limit is somewhere in the thousand cell range (i.e., if you have a thousand QUERY functions, you’re gonna have a bad time). This might sound like a lot, but if you have 50 rows of dates and 20 columns of metrics to calculate (as I did), you’ll discover it’s not as much as you thought.

another alternative which can work to generate a single conditional, rather than a series of them (which should be enclosed in parentheses), is to reverse the sense of the check, using a construction like this, instead:

TEXTJOIN(, TRUE, UNIQUE()) contains A

where ‘A’ is a column designation from the query.

It should be tested in your specific usage to see that it works with the particular nature of your across the values of ‘A’, but it has proved handy, too. It works best where A does not appear as a substring in multiple values of value_range (unless that’s what you’re going for, in which case this serves as a “like” condition across multiple values).

The value it provides is that you can compute the left hand list as an intermediate value in your sheet (and show it), if it does not change across the query, and is fairly easy for a human to read, in contrast to the list of ORs which changes with each value of ‘A’.

Another difference you should note between SQL and the Query function is the absence of a concatenation operator or function. It really is something they should add.
This sounds small, as the explanation given is that you just have to add an extra column in your dataset to perform the concatenation, however this ignores the headache this involves when dealing with remote datasources (importRange, e.g.). It requires the maintenance of a second, modified copy of the dataset where one would otherwise not be necessary, and the annoyance of the resulting revision churn on the file.

Hey!
So I’ve got my query function all setup but then I add columns to the sheet and the function doesn’t update (changing to the new column letter) like it normally would without the query. Is there a workaround for this?

Is there any hack or correct way of using “case when” in Query – there are places when you have to use them in a select statement for multiple conditions. Let me know if you find a way – I’ve been trying and wasn’t able to figure it out – you can even add it to your post if you find it.

Hmm I know you can’t use actual CASE logic in Sheets queries, but you could potentially use an ARRAYFORMULA and IF function when building the data range (before the select statement) to have conditional formulas run.

Hi all,
Im trying to build the Query formula based in other cells.. let me explain it to you:

This is the formula that is now working to me:
query({importrange($G$1,”Disney – Current!A:G”);importrange($G$1,”Disney – TVOD!A:G”);importrange($G$1,”Lionsgate – Current!A:G”);importrange($G$1,”Lionsgate – TVOD!A:G”);importrange($G$1,”Paramount – Current!A:G”)},”select * where Col1””,1)

As you can see the Importrange is calling the G1 to get the Key from the sheet. That Cell is a vlookup connected to a drop down. (Basically, I want to choose different sheets.)
But I have a problem, As you can see the Importrange formula is manually set by the name and range of the sheet that I’m calling. But not all the sheets are having the same tabs…
So, I build a table with all the tabs from the different sheets and create in a table the range part conected to the same drop down that I’m using to the key.
Basically what I need at the end is something like this:

I have been trying to use the “drop down” query without success. My data is on “Sheet 1” and is named “NTBA_2018_Roster”. My “validated range” drop down menu is located on “Sheet 2” in “Cell A2″. I am attempting to use the following query:

=query(‘Form responses 1’!A2:M, “SELECT COUNT(Col5) WHERE Col5 = ‘” & E6 & “‘ label COUNT(Col5)” “) returns #VALUE! and Error message: “Error
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: Col5″… also tried it with (E) instead but Returns #N/A and Error message: “Error
Query completed with an empty output.” instead of 0 so i guess that one would work if i could get 0 returned instead of #N/A. Any ideas?

Hey Commish I am trying to get the query to eliminate any cel that has the word delivered in it across all the sheets. It works only for the first sheet when I run this and eliminates the results from the rest of the sheets? Any ideas

Thank you for these awesome tutorials!!! I’m building an automated Performance matrix (combination of query and app script) and these tutorials helped a truckload!!! Struggled my bum off in VBA and abandoned the project. Then Google Sheets and JavaScript (App Script) happened…and the rest is history.

I thought I provided a comment (request) but somehow it didn’t make through.
Here I try one more time.

First, your tutorial is awesome, thanks for educational material!

second, I’m trying to built a query for a timestamped data. E.g. I’m building sort of totalizator, where a user can have many guesses until certain date (deadline, defined in a separate cell).
all the guesses from all the participating users shall be collected via Google form, which has a timestamp functionality implemented, to a separate Gsheet.

once this is done, then a query should select specific user’s (all users listed on list) last valid guess before the deadline.

Currently my query works for a single user:
=QUERY(Responses, “select C, D where C = ‘”&Sheet2!C3&”‘and B <= "&Deadline&" ORDER BY B desc LIMIT 1",1)
(I also converted timestamp to a numeric format by array formula on a separate sheet, also my deadline is converted to numeric format, so I don't need to use 'date' trick).
in the query: C-user, B- timestamp, D- guess.
as said, it works for a single user, but not for the list of users

Now I am just getting silly!
I have a Pilot Log which I want to enter into Google Sheets.
I need to have totals calculated in sheet 2 from every 7 rows of entries on sheet 1
I can’t remember how to get there.

Hello David. I started using Google Sheets to replace my older personal financial app, Moneydance. In the process, I looked at Tiller spreadsheets to see if they would function as well. When all was said and done, I opted for Google Sheets and preparing my own set of individual spread. But that turned out to be an interim “fix”.

During that time I ran across your web site, Codingisforloser, and took a hard look at what you had done. Your advice got me out of a whole mess of problems and I managed to get my personal financial spreads up in a little less than two weeks. My most recent development was implementing the pull down menus for my various reports.

And you were right about the power of the “query”. What originally was separate spreads has become a “database” using ‘query’s. All of this from an “ancient” (over 70) investor. Once again, thanks for the web site and the advice.

Hi Commissioner, I’m back with (I hope) a minor problem. I’d like to create one Google Sheet statement that pulls an amount of money out of my bank account and credits my specified credit card account. Right now I have two statements in my “Transactions” Google Sheet, one decreasing my bank account GS (Google Sheet0, and another one “paying off” my credit card account GS. You’d think that one could get by with one GS statement to do both operations at one time.

Hi Commisioner,
Congratulations for the post: very complete!
I’m finding problems with the QUERY function and converting cells that have a HYPERLINK function; these cells lose the link.
Is there any solution?

Hi Commisioner,
I wonder which functions could be used to calculate a field in a query. I am using additional column in the worksheet to calculate: =if(eomonth(F2;0)<eomonth(today();-5);eomonth(today();-5); eomonth(F2;0)), F2 being a date.
Is there a way to include such calculation in the SELECT? I tried to use it directly, but no success:
SELECT A, B, C, D, E, if(eomonth(F;0)<eomonth(today();-5);eomonth(today();-5); eomonth(F;0)

Hey Gasper, I wouldn’t recommend embedding a formula like that within a QUERY – instead either add that formula as a column in your data *before* you query it, or after you query it (will just keep your formulas more readable).