SELECT knowledge FROM sql_resources WHERE category='sql' AND slug='where'

WHERE

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.

Loading code...

In this article the code snippets are written in the Google BigQuery Standard SQL syntax.

Simple WHERE expressions

In these examples, the predicate expression in the WHERE clause is a simple boolean expression - an expression that returns true or false.

Loading code...

Inequality

numbers
3
4
5

IS NOT NULL

numbers
2
3
4
numbers
null
null
numbers
2
3
4

Compound WHERE expressions

In the following examples, the WHERE clause contains more complex predicate expressions, using the building blocks of the AND and OR logical operators.

Loading code...
strings
ab
aa
bb

SELECT statements

numbers
3
4
5

WHERE expressions on dates

When 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

Inequality

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
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...