- Categories:
WHERE¶
The WHERE clause specifies a condition that acts as a filter. You can use the WHERE clause to:
Syntax¶
Parameters¶
predicateA Boolean expression. The expression can include logical operators, such as
AND,OR, andNOT.
Usage notes¶
Predicates in the WHERE clause behave as if they are evaluated after the FROM clause (though the optimizer can reorder predicates if it does not impact the results). For example, if a predicate in the WHERE clause references columns of a table participating in an outer join in the FROM clause, the filter operates on the rows returned from the join (which might be padded with NULLs).
Use care when creating expressions that might evaluate NULLs.
In most contexts, the Boolean expression
NULL = NULLreturns NULL, not TRUE. Consider using IS [ NOT ] NULL to compare NULL values.In a
WHEREclause, if an expression evaluates to NULL, the row for that expression is removed from the result set (that is, it is filtered out).
The maximum number of expressions in a list is 200,000. For example, the limit applies to the number of expressions in the following SELECT statement:
To avoid reaching the limit, perform a join with a lookup table that contains the expression values, rather than specifying the values using the IN clause. For example, when the expression values in the previous example are added to a lookup table named
mylookuptable, you can run the following query successfully even if the lookup table has more than 200,000 rows:
Joins in the WHERE clause¶
Although the WHERE clause is primarily for filtering, the WHERE clause can also be used to express many types
of joins. For conceptual information about joins, see Working with joins.
A WHERE clause can specify a join by including join conditions, which are Boolean expressions that define which row(s) from one
side of the JOIN match row(s) from the other side of the join.
The following two equivalent queries show how to express an inner join in either the WHERE or FROM clause:
Outer joins can be specified by using either the (+) syntax in the WHERE clause or
the OUTER JOIN keywords in the FROM clause.
When you specify an outer join with (+), the WHERE clause applies (+) to each join column of the table that is
“inner” (defined below).
Note
The result of an outer join contains a copy of all rows from one table. In this topic, the table whose rows are preserved is called the “outer” table, and the other table is called the “inner” table.
In a LEFT OUTER JOIN, the left-hand table is the outer table and the right-hand table is the inner table.
In a RIGHT OUTER JOIN, the right-hand table is the outer table and the left-hand table is the inner table.
The following queries show equivalent left outer joins, one of which specifies the join in the FROM clause and one of which
specifies the join in the WHERE clause:
In the second query, the (+) is on the right hand side and identifies the inner table.
Sample output for both queries is below:
If you are joining a table on multiple columns, use the (+) notation
on each column in the inner table (t2 in the example below):
Note
There are many restrictions on where the
(+)annotation can appear; FROM clause outer joins are more expressive. Snowflake suggests using the(+)notation only when porting code that already uses that notation. New code should avoid that notation.The restrictions include:
You cannot use the
(+)notation to createFULL OUTER JOIN; you can only createLEFT OUTER JOINandRIGHT OUTER JOIN. The following is not valid. The statement causes the following error message:SQL compilation error: Outer join predicates form a cycle between 'T1' and 'T2'.If a table participates in more than one join in a query, the
(+)notation can specify the table as the inner table in only one of those joins. The following is not valid becauset1serves as the inner table in two joins. The statement causes the following error message:SQL compilation error: Table 'T1' is outer joined to multiple tables: 'T3' and 'T2'.Note, however, that you can use
(+)to identify different tables as inner tables in different joins in the same SQL statement. The following example joins three tables:t1,t2, andt3, two of which are inner tables (in different joins). This statement performs:
A LEFT OUTER JOIN between
t1andt2(wheret2is the inner table).A LEFT OUTER JOIN between
t2andt3(wheret3is the inner table).
The (+) may be immediately adjacent to the table and column name, or it may be separated by whitespace. Both of the following
are valid:
A query can contain joins specified in both the FROM ... ON ... clause and the WHERE clause. However, specifying
joins in different clauses of the same query can make that query more difficult to read.
Support for joins in the WHERE clause is primarily for backwards compatibility with older queries that do not use
the FROM ... ON ... syntax. Snowflake recommends using FROM ... ON ... when writing new queries with joins.
For details, see JOIN.
Examples¶
Simple examples of filtering¶
The following show some simple uses of the WHERE clause:
This example uses a subquery and shows all the invoices that have smaller-than-average billing amounts:
Performing joins in the WHERE clause¶
To specify a join in the WHERE clause, list the tables to be joined in the FROM clause, separating the tables
with a comma. Specify the join condition as a filter in the WHERE clause, as shown in the following example:
Note
The comma operator is older syntax for INNER JOIN. The following statement shows the recommended way to
perform a join using newer syntax. The query below is equivalent to the query above:
This next section shows 3-table joins and shows the difference in behavior with 0, 1, or 2 (+) outer join
operators.
Before executing the queries, create and load the tables to use in the joins:
Execute a 3-way inner join. This does not use
(+)(or the OUTER keyword) and is therefore an inner join. The output includes only rows for which there is a department, project, and employee:Perform an outer join. This is similar to the preceding statement except that this uses
(+)to make the second join a right outer join. The effect is that if a department is included in the output, then all of that department’s projects are included, even if those projects have no employees:Perform two outer joins. This is the same as the preceding statement except that this uses
(+)to make both joins into outer joins. The effect is that all departments are included (even if they have no projects or employees yet) and all projects associated with departments are included (even if they have no employees yet). Note that the output excludes projects that have no department.
(Remember, however, that Snowflake recommends using the OUTER keyword in the FROM clause rather than using
the (+) operator in the WHERE clause.)