Google Sheets QUERY Function Advanced Application Examples 5 (In-depth Analysis of the select Clause)
This is the 5th installment of my note on the most powerful function in Google Sheets, the QUERY function.
👇 The previous QUERY function series are compiled in a magazine.
By the way, the previous installment of the QUERY function series introduced an advanced application (or rather, a stunt) using the select clause, a technique that calculates strings of formulas entered in other cells, which is eval-like or, in Excel terms, EVALUATE-like.
In last week's note, I summarized not the QUERY function, but updates to the table feature.
Basics of the QUERY function's select clause
This time
QUERY(data, query, [headers])
I will be conducting an in-depth analysis of the second argument of the QUERY function, the query syntax, and specifically the select clause that appears first.

From basic usage to points of caution and advanced application examples, this should be the most detailed guide anywhere! (Advanced application examples will be in the next installment.)
There is some overlap with the 3rd installment of the QUERY function series, "Google Sheets QUERY Function Advanced Application Examples 3 (Understanding the 2nd Argument Query Statement)", but since these are important points, please re-verify them!
First, let's understand the basics of the select clause this time. Advanced application examples will be in the next installment.
Note that handling dates, date-times, and times in the QUERY function is a bit unique, so I will not touch upon cases involving these this time and will summarize them separately.
The select clause is a clause for specifying the columns to output
The select clause in a query statement is a clause for specifying the columns to output as a result and their order from the data specified in the first argument.
By using the select clause, you can output only the columns you want in the order you want from the data in the first argument.
First, I have summarized 7 points for specifying columns with the select clause.
There are two ways to write the select clause: one by specifying column numbers like Col1, Col2..., and one by specifying column letters like A, B...
You must be careful with uppercase and lowercase letters when specifying columns in the select clause.
The select clause can be omitted. If omitted, it is the same as select *.
Column specification in the select clause must be done one by one; you cannot specify a range.
The select clause must be written at the beginning of the query statement (the order of the query statement is important).
The select clause allows you to specify the order of the output columns.
The select clause cannot specify the same column multiple times.
Let's explain each of these.
1. There are two ways to write the select clause: specifying column numbers like Col1, Col2, etc., and specifying column letters like A, B, etc.
In query statements, the string used to specify a column is called an “ID (identifier),” but in the case of the query statement for the Google Sheets QUERY function,
Specify columns by column letter A, B, C…
When the first argument of the QUERY function is a cell range (within the same spreadsheet)
Specify columns by column number Col1, Col2…
When the first argument of the QUERY function is either a cell range or an array
* When specifying multiple columns, separate them with commas (,).
There are these two ways of writing it.
As of April 2025, you cannot use structured references (table name[heading name]) of the table feature in query statements.
Let's look at the two writing methods with concrete examples.
For example, if you want to output only the data for the three columns "Salesperson," "Product," and "Sales" from the five columns of data on the left below using the QUERY function,

Salesperson ... Column A
Product ... Column C
Sales ... Column E
viewing it as
=QUERY(A1:E23,"select A,C,E")
This is one way to write it.

Sales Representative ... 1st column in data Col1
Product ... 3rd column in data Col3
Sales ... 5th column in data Col5
and consider it as
=QUERY(A1:E23,"select Col1,Col3,Col5")
There are two ways to write it like this.
Regarding the writing rules
・When writing the second argument, the query string, within the formula, enclose the entire string in double quotes ""
・Insert a half-width space after select
👆These are common to both.
As for how to use the two writing styles, previously, when the first argument of the QUERY function was
a range within the same spreadsheet (other sheets are also OK)
▶ Specify with select A,B,C…
In the case of an array such as data referenced from another spreadsheet via IMPORTRANGE
▶ Specify with select Col1,Col2,Col3…
This was the distinction: for a range, you used column letter specifications like A, B, C, and for an array, you used column number specifications like Col1, Col2, Col3.
However, from July 2023 onwards
a range within the same spreadsheet (other sheets are also OK)
▶ Specify with select A,B,C…
▶ Specify with select Col1,Col2,Col3… Both can be used
In the case of an array such as data referenced from another spreadsheet via IMPORTRANGE
▶ Specify with select Col1,Col2,Col3… only
It has been changed like this, and it is now possible to use the Col1,Col2 notation even for ranges that previously resulted in an error.
※ "Range" refers to a cell range within the same spreadsheet (other sheets are also acceptable) specified directly without using functions or formulas in between. (※ As an exception, there are some functions like INDEX that return a "range")
Therefore, the case where an error occurs in the select clause is,

when the first argument is an array {A1:E23} like this, and you specify the columns with letters like A, C, E in the select clause.
At this time
Unable to parse the query string for function QUERY parameter 2.NO_COLUMN: A
This message returns a #VALUE! error.
In other words, specifying column numbers, which can be used for both ranges and arrays, is more versatile than specifying column letters, which can only be used for ranges. That is what it means.
However, while column letters (A) can be specified with one character, Col1 requires four characters to specify, so the formula tends to become long, and you might sometimes wonder, 'Wait, which column is this?'
For simple QUERY formulas that target the same sheet range, remember that there are cases where it is better to use column letters.
For now, if you use data brought from another spreadsheet using IMPORTRANGE, which is often used in conjunction with QUERY, as the first argument, the first argument is an array, so you must use the Col1, Col2... specification.

Regarding the Col1, Col2 specification, note that this column number is counted starting from 1 for the leftmost column of the data in the first argument, and it does not mean that column A is 1 and column B is 2.

As in the image above, even though the first argument, which is the data range, is B:F, if you count from column A as 1, 2, 3 and specify Col6 because it is the 6th column, which is column F, it will result in an error because you are specifying the 6th column when there are only columns 1 to 5 (B to F).
Personally, I recommend getting used to using the Col1, Col2... column number specification as much as possible, as it can be used for both arrays and ranges, does not require you to worry about the position of the original data, and can be used with 'generating select clause text with formulas', which I will introduce next time!
2. You need to be careful about uppercase and lowercase letters when specifying columns in the select clause
As I mentioned last time, you need to be careful because column identifiers (IDs) strictly distinguish between uppercase and lowercase letters.
In query statements, select and where do not care about case, so SELECT or WHERE are also fine.

However, there is a strict rule that column letters like A or C must always be uppercase, and for column numbers, Col1, Col2 must have an uppercase C and lowercase ol.
If you specify column letters in lowercase like a, c, e,

or if you write Col1, Col3 as col1 or COL3, in all lowercase or all uppercase,

it will result in an error like this.
Also,
There is no space between select and the column identifier
Separating with a full-width space instead of a half-width space
Separating with a space instead of a comma when specifying multiple columns

These are common mistakes, so please be careful.
Furthermore, in cases where you are handling data with many columns, if you are using alphabetical column specifications, column BY might be interpreted as the reserved word by when it appears, which can cause an error.
Cases where column specification by ID (identifier) results in an error
For this reason as well, I recommend using the Col1, Col2... specification.
3. The select clause can be omitted. Omitting it is the same as select *
If you do not need to filter the output columns or change the order, and you want to output the data in the same column order as the first argument, you can use
select *
to output all columns in their original order.
However, since this produces the same result as omitting the select clause, there is no need to explicitly write select *.

In other words,
=QUERY(A1:E23,"select * where Col3 = 'Product B'")
👆 the formula
=QUERY(A1:E23,"where Col3 = 'Product B'")
👆 can have select * omitted like this
is what that means.
I personally prefer to omit it, but there is no problem with explicitly writing select *.
4. Columns in the select clause must be specified one by one. Range specification is not possible.

When specifying columns in the QUERY function's select clause, you must specify them one by one, separated by commas.
For example, even if you want to output only the 2nd to 10th columns (B-J) excluding the 1st column from data with 10 columns (A-J) using the QUERY function's select clause, as shown in the image above,
✖ =QUERY(A1:J11,"select B to J")
or
✖ =QUERY(A1:J11,"select Col2-Col10")
as in you cannot specify a range collectively.
It would be quite convenient if this were possible, though.
By the way, if you write Col2-Col10, the - is treated as a minus sign, and it will attempt to perform the calculation using the operator between columns that I will introduce later (subtracting the 10th column from the 2nd column). (In the image example, it results in an error because it is trying to use an arithmetic operator on a string-type column.)
So, you might think, why not just specify the range of the first argument as B1:J11 from the start? However, even if you don't want to output column A within the QUERY function,
・where clause you might want to use column A as a condition to filter the rows to output
・order by clause you might want to use column A as the key column for sorting
In such cases, you need to include column A in the first argument.
Even if it is tedious,

=QUERY(A1:J11,"select B,C,D,E,F,G,H,I,J")
or
=QUERY(A1:J11,"select Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10")
is what you need to write.
But this becomes difficult when the number of columns increases to 20 or 30. There is a technique to generate this column specification string using a formula!
I would like to introduce this in the next advanced application example.
5. Write the select clause at the beginning of the query statement (the order of the query statement is important)
As written in the official documentation,

there are rules for the order in which language clauses are written.
When writing select and where as in the formula earlier, it must be select first and where second. (Omission is allowed)
If you write it while ignoring the order, "It's strange for select to appear in a place like this!" an error will occur.

6. The select clause can specify (reorder) the order of output columns

The select clause can not only select and narrow down the columns to output, but also specify the order in which the columns are output, that is, the order of columns from the left.
=QUERY(A1:E23,"select Col3,Col5,Col1")
For example, if you specify it in the order select Col3,Col5,Col1 like this, you can set the column order to the order of columns 3, 5, and 1.
7. The select clause cannot specify the same column multiple times

Although the select clause can extract columns and manipulate their order, you cannot "specify the same column multiple times".
For example
=QUERY(A1:E23,"select Col1,Col1")
or
=QUERY(A1:E23,"select A,A,A")
even if you try to output Col1 (A) multiple times with an expression like
Error
Unable to parse query string for Function QUERY parameter 2. COLUMN_ONLY_ONCE: Col1
an error will be returned.
COLUMN_ONLY_ONCE As stated, it means you can only use a column once.
Well, considering that query statements are based on databases, columns with the same name (heading) cannot exist, which is natural, but it is troublesome because there are times when I want to do this for processing reasons.
There is a workaround, so I will introduce it next time.
The CHOOSECOLS function and the QUERY function's select clause
We now understand that the QUERY function's select clause can output only specified columns from the target data (range or array) and change their order.
But doesn't this seem similar to the CHOOSECOLS function nowadays?
The CHOOSECOLS function is a relatively new function that was imported from Excel to Google Sheets around March 2023.
CHOOSECOLS(array, column_number_1, [column_number_2])
Just like the select clause of the QUERY function, you can specify the column numbers to extract from the array (range) in the first argument and output them in the specified order.

=CHOOSECOLS(A1:E23,3,5,1)
Furthermore, unlike the select clause of the QUERY function, it is also possible to

=CHOOSECOLS(A1:E23,1,1,1,1)
specify the same column multiple times to output it repeatedly.
And what's more, you can also use negative values to specify columns from the back (the rightmost column).

=CHOOSECOLS(A1:E23,-1,-2)
Since you can also specify the column part as an array, you can combine it with the SEQUENCE function to

=CHOOSECOLS(A1:E23,SEQUENCE(5,1,5,-1))
easily reverse the order of the columns (sort columns in reverse order).
It's also nice that you don't have to add 'Col' every time like Col1, Col2 as you do in the QUERY function's select clause.
Being able to specify by numbers alone, specify the same column multiple times, specify from the opposite side with negative values, and even specify with an array generated by SEQUENCE... as expected of a function specialized for column manipulation, CHOOSECOLS is convenient.
Therefore, if you simply want to extract specific columns from the original data or sort columns in a specified order, you should currently use the CHOOSECOLS function.
However, this does not mean that the select clause is unnecessary if you have the CHOOSECOLS function.
As I will describe later, the QUERY function's select clause is an important clause that plays a major role in outputting calculation results between columns using operators, formatting column data with scalar functions, and further aggregating by column with aggregation functions, through a combination of techniques.
Following the basic column specification, let's take a look at these combined techniques!
What you can do by combining the QUERY function with the select clause
In the basics of the select clause, we learned that you can output only specified columns and reorder columns in a specified sequence from the data in the first argument.
Let's look at other things you can do by combining with the select clause.
What you can do by combining with the select clause, and its caveats
In the select clause, you can not only simply specify columns from the data in the first argument, but also aggregate, process, and calculate columns at the same time.
Furthermore, it is interesting that you can also generate columns by specifying values directly.
In summary, there are these four combined techniques for the QUERY function select clause.
Aggregate columns using aggregate functions
Process columns using scalar functions
Calculate columns using operators
Generate columns by specifying values
When using these, there is a caveat that headers are automatically generated.

Unfortunately, there is no way to control the generated headers, and you have no choice but to remove the headers using the label clause or other functions after they are generated.
I will not touch on this method for handling headers this time, but will explain it during the explanation of other clauses.
Let's look at the four combined techniques of the select clause.
select clause combined technique 1. Aggregate columns using aggregate functions
First, let's look at aggregate functions.
Aggregate functions can be used in the select clause

Since the QUERY function is fundamentally a function for aggregating data, five aggregate functions can be used in the query string of the second argument.
Many people likely have the impression that aggregate functions are used in conjunction with the group by clause, but in fact, aggregate functions can be used alone with just the select clause.

=QUERY(A:E,"select sum(Col5)")
The formula above calculates 16250, which is the sum of the sales column in the 5th column. An extra header is also included, but you can see that it matches the result of =SUM(E:E).
The aggregate function sum can be said to have the same behavior as the sheet function SUM is.
Well, you wouldn't normally use it this way, but this can actually be utilized in the advanced examples coming up next time.
For now, let's remember that you can use just select and an aggregate function.
The aggregate function count is not the COUNT function in sheet functions, but rather something closer to the COUNTA functioncloser to.

The COUNT function is a function that counts the number of numeric values in a range, and strings are not counted.
In the image above, you can see that =COUNT(C:C) targeting the string column C returns 0.
The function that counts the number of cells containing values, including strings, is the COUNTA function.
=COUNTA(C:C) returns 23.
Since the aggregate function count can be used for both numeric and string columns, it can be said to be close to COUNTA.
=QUERY(A:E,"select count(Col3)")
returns 22, which is one less than the 23 from =COUNTA(C:C), because it considers the first row as a header and excludes it.

Since both do not count blank cells, you can see that when two cells are left blank as shown above, the result for both decreases by 2.
The aggregate function avg works the same as the sheet function AVERAGE in concept.

Both return the average value of the data after excluding blank cells.
The difference in output results is because the QUERY function's automatic formatting differs from sheet functions. It is just a difference in whether the decimal places are rounded for display; the actual underlying values are the same.
The aggregate functions max and min can also be used on strings.
The QUERY function's aggregate functions max and min behave slightly differently than the sheet functions MAX and MIN.

They are the same when used on numeric columns.
As an aside, aggregate functions can also be specified in multiple ways separated by commas, just like column specifications.
=QUERY(A:E,"select max(Col5),min(Col5)")
You can write multiple items separated by commas like this.
I wrote above that the select clause cannot specify the same column multiple times, but in fact, if the processing method is different like this, you can use the same Col5 twice, such as max(Col5),min(Col5). (This is a key point).

max(Col5),max(Col5) will result in an error if the processing method is exactly the same.
Now, just like with numbers, the aggregate functions max and min for dates and date-times return the same results as sheet functions, but the difference arises when you specify a string type column.

The sheet functions MAX and MIN ignore strings and treat blanks as 0, so both return 0. This is because MAX and MIN return results by ignoring text and treating blank cells as 0.
In other words, sheet functions MAX and MIN cannot be used on strings.
On the other hand, as stated in the official documentation, the QUERY function's aggregate functions max and min look at strings in alphabetical order, judging A (small) to Z (large), and return the maximum and minimum values within the strings.
In this case, blank cells are ignored.

Using this in an easy-to-understand example

It will look like this.
=QUERY(Table_1,"select max(Col1),min(Col1),max(Col2),min(Col2)")
Simply put, this means you can retrieve the value that comes first when sorted in ascending order using min, and the value that comes first when sorted in descending order using max.
The aggregate functions max and min strictly distinguish between uppercase/lowercase letters and hiragana/katakana.

The QUERY function's aggregate functions max and min distinguish between uppercase and lowercase letters for alphabetical characters, and
they are treated such that "uppercase letters are smaller and lowercase letters are larger." (It's confusing.)
Also, they distinguish between hiragana and katakana in the same way, and
they are treated such that "hiragana are smaller and katakana are larger."
It's interesting, isn't it?
However, you need to be careful with kanji even in Japanese.

Unlike the results of the SORT function, be aware that the results will be far from the image of sorting by Japanese on-yomi readings.
As I touched on briefly before, this is because the sorting criteria for the QUERY function is based on Unicode values.
I would like to delve into this part again when explaining the order by clause.
By the way, max and min can be used not only for string types but also for Boolean values.

Since TRUE is treated as larger than FALSE, it doesn't seem like they are being compared as strings.
Well, I can't think of a case where this would be useful, but it means that the QUERY function's max and min can be used for all types.
Aggregate functions can only be used with column identifiers
They are very powerful aggregate functions for group aggregation and pivot aggregation, but unfortunately, they can only be used on column identifiers.

In other words,

As will be shown later,
=QUERY(A1:D12,"select Col4-Col3")
you can calculate the difference between the first and second half by performing arithmetic operations on columns like this,
=QUERY(A1:D12,"select max(Col4)")
or calculate the maximum value of sales in the second half, but
=QUERY(A1:D12,"select max(Col4-Col3)") ...This is ✖
max(Col4-Col3) this means that you cannot put anything other than a column (identifier) as an argument for an aggregation function like Col4-Col3.
If such calculations are necessary, you will need to use workarounds like nesting the QUERY function.
[Aside] Comparison with Excel's GROUPBY and PIVOTBY functions
In the query string of the QUERY function, only the following five aggregation functions are available:
sum
count
avg
max
min
You can only use these five.
Even if you want to use other functions, such as TEXTJOIN or INDEX during aggregation within the query string, it is not possible.
On the other hand, while Excel's latest aggregation functions GROUPBY and PIVOTBY commonly use eta-reduced lambdas like SUM or COUNTA as arguments to specify functions, by using the LAMBDA function, it is basically possible to use almost all sheet functions.
I think this is a difference in direction between Google Sheets' QUERY function, which stands apart from other SQL-like sheet functions that aggregate in a database-like manner, and Excel's GROUPBY and PIVOTBY functions, which are extensions of sheet functions, but
depending on the processing you want to do, it is good to remember that there are things that cannot be done with the QUERY function but can be easily done with GROUPBY or PIVOTBY.
I would like to write about this comparison separately.
select clause combination technique 2. Processing columns with scalar functions
Scalar functions are mostly used when the target column type is date, datetime, or time.
Since we are not dealing with dates or datetimes this time, let's touch on the few scalar functions available for string types of

upper and lower.
Unify to uppercase upper, unify to lowercase lower
The scalar functions upper and lower available in the QUERY function are functions that align alphabetic characters to uppercase for upper and lowercase for lower.

■ Unify all alphabetic characters in the first column to uppercase
=QUERY(A:A,"select upper(Col1)",1)
■ Unify all alphabetic characters in the first column to lowercase
=QUERY(A:A,"select lower(Col1)",1)
This behaves basically the same as the sheet functions UPPER function,LOWER function.
This is something that is more useful in the where clause and is not used much in the select clause.
upper and lower result in an error if applied to non-string types,

and if applied to characters other than alphabets (Japanese or numeric strings) or spaces, they have no effect (change) on the string and are output as is.

select clause combination technique 3. Calculating columns with operators

You can use arithmetic operators in query statements for combinations such as between columns or between a column and a single number.
As I mentioned in the third part of the QUERY function, there is actually an operator missing from the official documentation, which is % (modulo).
$$
\begin{array}{lll}
\text{No}&\text{演算子}&\text{解説}\\ \hline
\text{1}&\text{+}&\text{加算(足し算)}\\ \hline
\text{2}&\text{-}&\text{減算(引き算)}\\ \hline
\text{3}&\text{*}&\text{乗算(掛け算)}\\ \hline
\text{4}&\text{/}&\text{除算(割り算)}\\ \hline
\text{5}&\text{\%}&\text{剰余 (余り) 類似シート関数 : MOD}\\ \hline
\end{array}
$$
👆 So, this is the complete version of arithmetic operators that can be used in the select clause of the QUERY function. (Please let me know if there are any other operators that can be used)
The QUERY function can perform calculations row by row using operators

■ Addition, Subtraction
=QUERY(A2:D12,"select Col1+Col2,Col1-Col2")
■ Multiplication, Division
=QUERY(A2:D12,"select Col1*Col2,Col1/Col2")
■ Modulo (Remainder)
=QUERY(A2:D12,"select Col1%Col4")
When you use operators in the select clause of the QUERY function, you can output the results of calculations performed row by row between the specified columns.
Even when using operators, header rows are automatically generated just as they are when using aggregation functions or scalar functions.
sum(Col1Col2)
difference(Col1Col2)
product(Col1Col2)
quotient(Col1Col2)
modulo(Col1Col4)
This brings us to elementary school arithmetic, but you can also parentheses to control the order of operations.

=QUERY(A2:D12,"select Col1-Col2/Col4")
If you do this, naturally, division is calculated before subtraction, so the result will be a number with repeating decimals because Col2/Col4 is calculated and then subtracted from Col1.
By changing this to
=QUERY(A2:D12,"select (Col1-Col2)/Col4")
you can see that by calculating Col1-Col2 first and then dividing the result by Col4, you get a completely different result (all 3s).
Q1. I want to use operators in the QUERY function's select clause to get only the integer part of a division, just like the QUOTIENT sheet function.

This has been a long explanation, but since we're on the topic of elementary school arithmetic, let's try a simple challenge.
When you perform division using operators in the QUERY function, numbers after the decimal point appear.
The challenge is to output the integer part of the division result, in other words, the same result as dividing the first column by the second column using the QUOTIENT function (right side of the image) using the QUERY function.
Here is the data.
列1 列2
8 6
70 9
40 7
90 4
5 30
20 2
80 3
60 8
10 1
50 10How about it? Let's think back to elementary school arithmetic and give it a try!
↓↓
The answer starts here.
↓↓
A1. I want to use operators in the QUERY function's select clause to get only the integer part of a division, similar to the QUOTIENT sheet function.
Here is the answer.

=QUERY(A2:B12,"select (Col1-Col1%Col2)/Col2")
Here, the hidden operator % (Modulo) and parentheses play a key role.
If you recall division from elementary school,
Dividend ÷ Divisor = Quotient ... Remainder
It looks like this formula. If you rewrite this,
Dividend = Divisor × Quotient + Remainder
This is what you get. Since this quotient part is the integer part of the division, which is the result when using the QUOTIENT sheet function,
Quotient = ( Dividend - Remainder ) ÷ Divisor
This means you just need to calculate it this way.
Within the QUERY function, the dividend is Col1, the divisor is Col2, and the remainder is % so it can be calculated as,
( Col1 - Col1 % Col2 ) / Col2
which results in this.
In Excel or Google Sheets, you would normally use the ROUNDDOWN function, the INT function, or the TRUNC function to quickly truncate decimals, but doing so with operators is quite cumbersome.
However, this means that even in calculations inside the QUERY function where the above sheet functions cannot be used, you can obtain the same result as the QUOTIENT function by making full use of operators!
Operators can also use direct numerical values or the results of aggregation functions in addition to columns

Operators within the QUERY function can be used for calculations not only with columns (identifiers) but also by specifying numerical values directly.
=QUERY(A2:D12,"select Col1+1,Col1-5,Col1*3")
By writing it this way, it is possible to add 1, subtract 5, or multiply by 3 for each value in column 1.
This direct specification of numerical values can also use cell references or the results of formulas; in that case, you close the string beforehand with a "double quote and concatenate with &.

=QUERY(A2:D12,
"select Col1+"&K1&",Col1-"&L1&",Col1*"&MAX(K1:M1))
Let's confirm that the string part is properly colored green in the formula bar.

Also, operators can be used on the results of aggregation functions.

As written earlier, aggregation functions can only take columns (identifiers) as arguments, so
✖ =QUERY(A2:D12,"select sum(Col1*Col2)")
👆This will result in an error, but
◎ =QUERY(A2:D12,"select sum(Col1)*sum(Col2)")
👆This works fine.
[Note] Operators can only be used with numeric types

While aggregate functions perform calculations by looking vertically, operators are features that perform calculations by looking horizontally.
Therefore, because column types may be mixed in some cases, a point to note is that if even one column that is not a numeric type is included with an operator, it will result in a total error.
The formula above results in an error because the first column, Col1, is a string type.

In Excel or Google Sheets, boolean values like TRUE/FALSE or dates feel like they can be treated as numbers in calculations using operators, but in QUERY function query statements, they are strictly distinguished from numeric types and operators cannot be used.
The most troublesome case is when blanks (Null) are mixed in.

When using an operator in a QUERY function to calculate a numeric column that contains blanks, blanks are not treated as 0 or ignored; instead, the calculation result for rows containing blanks simply becomes blank.
This is problematic... Null is too powerful!!
As mentioned earlier, in the case of aggregate functions, they ignore blanks and perform the aggregation.
Thus, when performing column operations with the QUERY function on data that includes blank cells,

you need to fill the blanks with 0 at the first argument stage.
The example uses the N function, but since N(A2:C12) converts header row text to 0, it is more careful to use IF(A2:C12="",0,A2:C12) instead.
In any case, when performing array calculations in the first argument, you need to combine it with the ARRAYFORMULA function.
[Aside] If only string concatenation could be used with operators in the select clause...
The QUERY function is extremely convenient, but what I feel is that its string manipulation is weak.
Even with this select clause operator, like in GAS (JavaScript), if you could concatenate with + even if string types are mixed...

It would be super convenient if I could concatenate strings like this....
Unfortunately, you cannot concatenate strings within a query statement.
select clause combination technique 4: Generating columns by specifying values

Although not officially stated, in the QUERY function's select clause, you can generate and output columns that do not exist in the first argument by specifying values directly.
I mentioned earlier that you can combine numbers directly using operators like Col1*5, but you can also specify the number 5 directly without using operators.
Headers are also automatically generated in this case. Let's look at the unique behavior when specifying values directly.
Behavior when specifying values in the select clause

Values specified in the select clause are expanded to the same number of rows as the first argument.
What is interesting about this is that even if you do not select any columns from the first argument in the select clause, the values are expanded to the number of rows in the first argument.

=QUERY(A2:C12,"select 5,10,20")
For example, if you do not select the first argument and use select 5,10,20, 5, 10, and 20 will be repeated for the number of rows in the first argument's data (in this case, 10 rows).
In other words, if you generate a dummy array of 10 rows using the SEQUENCE function and specify it as the first argument,

=QUERY(SEQUENCE(10),"select 1,2,3")
you can easily expand multiple values downwards for a certain number of rows like this.
It is also possible to specify strings directly in the select clause instead of numbers,

=QUERY(SEQUENCE(10),"select 'jump','spin'")
and for strings, the rule is to enclose them in single quotes '.

=QUERY(,"select 1,2,3")
I mentioned that values specified directly in the select clause are repeated downwards for the number of rows in the first argument, but if the first argument is empty, it is treated as having one row of Null, so only the header and one row are returned.
Also, similar to specifying columns, when specifying values directly,

specifying the same value twice or more will result in an error.
Generating blank columns in the select clause

In the select clause, you can specify individual values directly, but you cannot specify blanks or empty strings.
=QUERY(A2:C12,"select Col1,'',Col2")
results in an #N/A error,
=QUERY(A2:C12,"select Col1,,Col2")
results in a #VALUE! error due to a query syntax error.
As a workaround, you can insert a space, but

=QUERY(A2:C12,"select Col1,' ',Col2")
this only looks blank visually, and the content is a repetition of cells containing a space. If you check with the ISBLANK function, it will return FALSE (not blank).
Therefore, as a way to insert blanks in the select clause of the QUERY function, I, mir, recommend

=QUERY(A2:C12,"select Col1,1/0,Col2")
1/0 to generate blanks.
I introduced this in the previous part of the QUERY function series, in the explanation of how to eval in the select clause.
Explanation 4-1: A trick to generate blank columns in the select clause of the QUERY function
Since this is just the range where the formula is expanded, you cannot manually type values directly into these blank cells, but it might be useful when you need blanks for printed materials or for copying and pasting values.
Next time: Advanced application examples of the QUERY function's select clause
This time, I wrote about the basics of the QUERY function's select clause, what it can do, and what it cannot do.
Next time, building on this content, I will move into how to generate the select clause using formulas and advanced application examples such as tricks using the select clause.
I never expected that even just the select clause wouldn't be finished in one go...
いいなと思ったら応援しよう!
チップ大歓迎です。やる気がアップしますw