Google Sheets QUERY: Conditions

WHERE

In a query, the WHERE keyword is used to add one or more conditions.

For example, the query SELECT * WHERE D = E literally means:

"SELECT data from all columns WHERE the value of column D is equal to the value of column E"


Adding a condition

Enter WHERE followed by the condition.

For example to select all rows (*) whose rank (column D) = 0, enter:

=QUERY(DB!A1:G15,"SELECT * WHERE D = 0")
google sheets function query select where

In this case, all ranks (column D) will necessarily be 0 since this is the condition, so it is not necessarily useful to display this column in the results.

To avoid displaying column D, enter the other columns (instead of *):

=QUERY(DB!A1:G15,"SELECT A, B, C, E, F, G WHERE D = 0")
google sheets function query select where columns

Adding multiple conditions

When the query contains multiple conditions, add AND or OR between the conditions.

To select all rows (*) whose rank (column D) = 0 AND the number of messages (column E) = 0, enter:

=QUERY(DB!A1:G15,"SELECT * WHERE D = 0 AND E = 0")
google sheets function query select where conditions

It is also possible to add parentheses to conditions.

For example, to display the same results as before but also show any user whose name is "Mason", enter:

=QUERY(DB!A1:G15,"SELECT * WHERE (D = 0 AND E = 0) OR C = 'Mason'")
google sheets function query select where and or
Text values (like Mason here) must be placed in quotes.

Using operators

It is also possible to use the + - * / % operators in a query (% = modulo, this operator returns the remainder of a division).

For example, to display the ID (A), the username (C), and the number of messages (E) multiplied by 2, but only for every other row, enter:

=QUERY(DB!A1:G15,"SELECT A, C, E * 2 WHERE A % 2 = 0")
google sheets function query modulo where