WHERE
The WHERE
clause filters query results based on specified conditions. It allows you to retrieve only the rows that satisfy the filtering criteria, enabling precise data selection.
Elements
condition
-
An expression that evaluates to true or false. Only rows for which the condition evaluates to true are included in the result.
Supported operators
Comparison operators
The WHERE clause supports various comparison operators for creating conditions:
Operator | Description | Example |
---|---|---|
Equals - Tests if values are equal |
|
|
Not equals - Tests if values are not equal |
|
|
Greater than - Tests if left value is greater |
|
|
Greater than or equal to |
|
|
Less than - Tests if left value is smaller |
|
|
Less than or equal to |
|
Logical operators
Logical operators combine multiple conditions:
Operator | Description | Example |
---|---|---|
Requires both conditions to be true |
|
|
Requires at least one condition to be true |
|
|
Negates a condition |
|
Set membership operators
Check if values are members of sets:
Operator | Description | Example |
---|---|---|
Tests if a value matches any in a list |
|
|
Tests if a value matches any in an array column |
|
Null testing
Operators for checking null values:
Operator | Description | Example |
---|---|---|
Tests if a value is NULL (missing) |
|
|
Tests if a value is not NULL (present) |
|
Pattern matching
Operators for string pattern matching:
Operator | Description | Example |
---|---|---|
Pattern matching with wildcards |
|
Examples
Basic filtering
SELECT * FROM products
WHERE category = 'Electronics'
SELECT * FROM products
WHERE price < 100
SELECT * FROM customers
WHERE joinDate > '2023-01-01'
Multiple conditions
SELECT * FROM products
WHERE category = 'Electronics' AND price < 500 AND inStock = true
SELECT * FROM customers
WHERE region = 'Europe' OR region = 'Asia'
SELECT * FROM products
WHERE (category = 'Electronics' OR category = 'Computers')
AND price < 1000
AND NOT discontinued = true
Working with NULL values
SELECT * FROM customers
WHERE phoneNumber IS NULL
SELECT * FROM customers
WHERE address IS NOT NULL AND address.street IS NOT NULL
Using sets and arrays
SELECT * FROM products
WHERE category IN ('Electronics', 'Computers', 'Accessories')
SELECT * FROM products
WHERE category IN (:category1, :category2, :category3)
SELECT * FROM products
WHERE :searchTag = ANY(tags)
SELECT * FROM customers
WHERE status = ANY(:statusList)
Notes
-
Conditions in the WHERE clause are applied before any GROUP BY, ORDER BY, or LIMIT operations
-
For complex conditions, use parentheses to explicitly control the order of evaluation
-
Type compatibility is enforced - you cannot directly compare values of different types
-
NULL values require special handling with IS NULL and IS NOT NULL - standard comparison operators don’t work with NULL
-
The LIKE operator requires a non-wildcard prefix or suffix for optimal performance
-
For pagination efficiency, ensure your WHERE conditions match available indexes
Related features
-
Operators overview - Complete reference for all operators
-
text_search() function - Advanced text search capabilities
-
JOIN clause - Combining data from multiple tables
-
FROM clause - Specifies the source table
-
Data types - Type compatibility in conditions
-
Optional fields - Working with null values