WHERE

constructsupportedQueries & DML

docs.snowflake.com ↗

The WHERE clause specifies a condition that acts as a filter. You can use the WHERE clause to: - Filter the result of the FROM clause in a SELECT statement. - Specify which rows to operate on in an UPDATE, MERGE, or DELETE .

Syntax

...
WHERE <predicate>
[ ... ]

Parameters

NameRequiredDescription
predicateA Boolean expression. The expression can include logical operators, such as AND, OR, and NOT.

Usage notes

Parameters

predicate: A Boolean expression. The expression can include logical operators, such as AND, OR, and NOT.

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 create FULL OUTER JOIN; you can only create LEFT OUTER JOIN and RIGHT 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'. Copy code -- NOT VALID select t1.c1 from t1, t2 where t1.c1 (+) = t2.c2 (+); - 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 because t1 serves 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'. Copy code -- NOT VALID select t1.c1 from t1, t2, t3 where t1.c1 (+) = t2.c2 and t1.c1 (+) = t3.c3; 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, and t3, two of which are inner tables (in different joins). This statement performs: A LEFT OUTER JOIN between t1 and t2 (where t2 is the inner table). - A LEFT OUTER JOIN between t2 and t3 (where t3 is 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.