The WHERE clause is one of the fundamental building blocks of a SQL SELECT statement. In this article we'll explain how it works.
The WHERE clause is one of the fundamental building blocks of a SQL SELECT statement. In this article we'll explain how it works.
The WHERE clause is formed of the reserved 'where' keyword followed by what is known as a predicate expression - simply an expression which returns true or false.
If, for a given row, the predicate returns true, that row will be included in the output of the query.
If the predicate returns false, that row will be excluded.
Let's look at some examples.
-- Generic SQL SELECT statement
select
-- (some columns)
from
-- (some tables)
where
-- (A 'predicate' expression to be used as a filter)
group by
-- (some columns)In this article the code snippets are written in the Google BigQuery Standard SQL syntax.
WHERE expressionsIn these examples, the predicate expression in the WHERE clause is a simple boolean expression - an expression that returns true or false.
with my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
numbers > 2 -- This is the predicate expression| numbers |
|---|
| 3 |
| 4 |
| 5 |
| numbers |
|---|
| 2 |
| 3 |
| 4 |
| numbers |
|---|
| null |
| null |
| numbers |
|---|
| 2 |
| 3 |
| 4 |
WHERE expressionsIn the following examples, the WHERE clause contains more complex predicate expressions, using the building blocks of the AND and OR logical operators.
with my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
numbers = 2| strings |
|---|
| ab |
| aa |
| bb |
| numbers |
|---|
| 3 |
| 4 |
| 5 |
WHERE expressions on datesWhen using dates and times, all of the same rules apply. The predicate expression in the WHERE clause must still return true or false, but you are free to use functions and compound logical expressions.
| dates |
|---|
| 2020-01-01 |
| dates |
|---|
| 2020-01-06 |
| 2020-01-07 |
| 2020-01-08 |
| 2020-01-09 |
| 2020-01-10 |
| dates |
|---|
| 2020-01-02 |
| 2020-01-03 |
| 2020-01-04 |
| dates |
|---|
| 2020-01-05 |
with my_table as (select * from unnest([null,2,3,4,null]) as numbers)
select numbers from my_table
where
numbers is not nullwith my_table as (select * from unnest([null,2,3,4,null]) as numbers)
select numbers from my_table
where
numbers is nullwith my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
numbers in (1,2)with my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
numbers between 2 and 4 -- Note - these limits are inclusivewith my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
-- Use brackets to control precedence
numbers > 2 and (numbers < 3 or numbers > 4)with my_table as (select * from unnest(['a', 'b', 'ab', 'aa', 'bb', 'cc']) as strings)
select strings from my_table
where
-- Predicate expressions can contain functions
(strings like 'a%' or strings like 'b%') and length(strings) = 2with my_table as (select * from unnest([1,2,3,4,5]) as numbers)
select numbers from my_table
where
-- Predicate expressions can contain other select statements
numbers > (select 1+1)with my_table as (
select * from unnest(generate_date_array('2020-01-01', '2020-01-10')) as dates
)
select dates from my_table
where
dates = '2020-01-01'with my_table as (
select * from unnest(generate_date_array('2020-01-01', '2020-01-10')) as dates
)
select dates from my_table
where
dates > '2020-01-05'with my_table as (
select * from unnest(generate_date_array('2020-01-01', '2020-01-10')) as dates
)
select dates from my_table
where
-- Note - between is inclusive
dates between '2020-01-02' and '2020-01-04'with my_table as (
select * from unnest(generate_date_array('2020-01-01', '2020-01-10')) as dates
)
select dates from my_table
where
extract(day from dates) = 5 and extract(month from dates) = 1