Skip to content

WHERE ​

The WHERE clause selects the input rows used by a query. A row is retained only when its condition is SQL true. False and NULL results are excluded.

WHERE is evaluated before grouping. To filter an aggregate result, use HAVING.

Syntax ​

text
SELECT expressions
FROM source
WHERE condition

Combine conditions with AND, OR and NOT. Parentheses make the intended precedence explicit. Comparisons can use columns, expressions, parameters and subqueries.

Examples ​

Selecting matching orders ​

sql
CREATE TABLE orders (id INTEGER, status TEXT, amount INTEGER);
INSERT INTO orders VALUES (1, 'paid', 100), (2, 'pending', 200), (3, 'paid', 40);
sql
SELECT id, amount
FROM orders
WHERE status = 'paid' AND amount >= 50
ORDER BY id
FORMAT csv;
text
1,100

Testing missing values ​

value = NULL does not select missing values. Use IS NULL:

sql
CREATE TABLE contacts (id INTEGER, email TEXT);
INSERT INTO contacts VALUES (1, 'ada@example.org'), (2, NULL);
sql
SELECT id
FROM contacts
WHERE email IS NULL
FORMAT csv;
text
2

See IN for membership tests and comparison functions for related predicates.