Skip to main content

WHERE

Query syntax

Filters the rows of the FROM source and its joins before any grouping. A row is kept only when the condition is true; false and NULL both drop it.

Syntax​

SELECT ...
FROM <source> [ <alias> ]
[ <join> ... ]
WHERE <condition>
[ GROUP BY ... ]
...

Parameters​

ParameterDescription
<condition>A boolean expression: comparisons, AND, OR, NOT, IN, EXISTS, BETWEEN, LIKE, IS NULL, a boolean column, or a function that returns a boolean such as regexp_like. See Operators.

Usage notes​

  • A comparison with NULL is unknown, so o.status <> 'closed' does not keep rows whose status is NULL. Add OR o.status IS NULL to keep them.
  • In a text, date or timestamp column an empty string is NULL.
  • A plain column compared with a literal (o.status = 'open', o.amount > 100, o.region IN ('AU', 'NZ')) is pushed into the read of the table, so only matching rows are read. Expressions are pushed too when the data store can evaluate them the same way; otherwise they run after the rows are read.
  • An unquoted number compared with a text column compares as a number: t.code = 7 matches '7' and '07'.
  • WHERE runs before GROUP BY. To filter on an aggregate, use HAVING. To filter on a window function, compute it in a derived table and filter outside.
  • A subquery can appear as IN (SELECT ...), EXISTS (SELECT ...) or <column> <op> (SELECT ...). See Subqueries.

Examples​

SELECT o.key, o.amount
FROM Order o
WHERE o.status IN ('open', 'held')
AND o.amount >= 1000
AND (o.title ILIKE '%refund%' OR o.closed_at IS NULL)
keyamount
PO-10011200
PO-10174500

A condition over an expression:

SELECT t.path FROM tables.drive.file t
WHERE lower(split_part(t.path, '/', 1)) = 'finance' AND length(t.name) > 20

Rows whose value is not in another table:

SELECT c.key FROM Customer c
WHERE NOT EXISTS (SELECT r.to_key FROM placed r WHERE r.from_key = c.key)

Not supported​

RefusedWrite instead
An aggregate in WHEREFilter on it in HAVING
A window function in WHERECompute it in a derived table and filter outside
-- Refused by the engine
SELECT o.customer FROM Order o WHERE COUNT(*) > 5 GROUP BY o.customer
-- Refused by the engine
SELECT o.key FROM Order o WHERE ROW_NUMBER() OVER (ORDER BY o.amount DESC) <= 3

See also​

SELECT · HAVING · Operators · IN · EXISTS · LIKE · BETWEEN · IS NULL