WHERE clause allows you to filter the data that comes from theFROM clause of SELECT.
If there is a WHERE clause, it must be followed by an expression of type UInt8.
Rows where this expression evaluates to 0 are excluded from further transformations or the result.
The expression following the WHERE clause is often used with comparison and logical operators, or one of the many regular functions.
The WHERE expression is evaluated on the ability to use indexes and partition pruning, if the underlying table engine supports that.
PREWHEREThere is also a filtering optimization called
PREWHERE.
Prewhere is an optimization to apply filtering more efficiently.
It is enabled by default even if PREWHERE clause is not specified explicitly.Testing for NULL
If you need to test a value for NULL, use:
IS NULLorisNullIS NOT NULLorisNotNull
NULL will otherwise never pass.
Filtering data with logical operators
You can use the following logical functions together with theWHERE clause for combining multiple conditions:
Using UInt8 columns as a condition
In ClickHouse,UInt8 columns can be used directly as boolean conditions, where 0 is false and any non-zero value (typically 1) is true.
An example of this is given in the section below.
Using comparison operators
The following comparison operators can be used:| Operator | Function | Description | Example |
|---|---|---|---|
a = b | equals(a, b) | Equal to | price = 100 |
a == b | equals(a, b) | Equal to (alternative syntax) | price == 100 |
a != b | notEquals(a, b) | Not equal to | category != 'Electronics' |
a <> b | notEquals(a, b) | Not equal to (alternative syntax) | category <> 'Electronics' |
a < b | less(a, b) | Less than | price < 200 |
a <= b | lessOrEquals(a, b) | Less than or equal to | price <= 200 |
a > b | greater(a, b) | Greater than | price > 500 |
a >= b | greaterOrEquals(a, b) | Greater than or equal to | price >= 500 |
a LIKE s | like(a, b) | Pattern matching (case-sensitive) | name LIKE '%top%' |
a NOT LIKE s | notLike(a, b) | Pattern not matching (case-sensitive) | name NOT LIKE '%top%' |
a ILIKE s | ilike(a, b) | Pattern matching (case-insensitive) | name ILIKE '%LAPTOP%' |
a BETWEEN b AND c | a >= b AND a <= c | Range check (inclusive) | price BETWEEN 100 AND 500 |
a NOT BETWEEN b AND c | a < b OR a > c | Outside range check | price NOT BETWEEN 100 AND 500 |
Pattern matching and conditional expressions
Beyond comparison operators, you can use pattern matching and conditional expressions in theWHERE clause.
| Feature | Syntax | Case-Sensitive | Performance | Best For |
|---|---|---|---|---|
LIKE | col LIKE '%pattern%' | Yes | Fast | Exact case pattern matching |
ILIKE | col ILIKE '%pattern%' | No | Slower | Case-insensitive searching |
if() | if(cond, a, b) | N/A | Fast | Simple binary conditions |
multiIf() | multiIf(c1, r1, c2, r2, def) | N/A | Fast | Multiple conditions |
CASE | CASE WHEN ... THEN ... END | N/A | Fast | SQL-standard conditional logic |
Expression with literals, columns or subqueries
The expression following theWHERE clause can also include literals, columns or subqueries, which are nested SELECT statements that return values used in conditions.
| Type | Definition | Evaluation | Performance | Example |
|---|---|---|---|---|
| Literal | Fixed constant value | Query write time | Fastest | WHERE price > 100 |
| Column | Table data reference | Per row | Fast | WHERE price > cost |
| Subquery | Nested SELECT | Query execution time | Varies | WHERE id IN (SELECT ...) |
Examples
Testing for NULL
Queries with NULL values:
Filtering data with logical operators
Given the following table and data:AND - both conditions must be true:
OR - at least one condition must be true:
NOT - Negates a condition:
XOR - Exactly one condition must be true (not both):
AND, OR, NOT, XOR) is generally more readable, but the function syntax can be useful in complex expressions or when building dynamic queries.
Using UInt8 columns as a condition
Taking the table from a previous example, you can use a column name directly as a condition:Using comparison operators
The examples below use the table and data from the example above. Results are omitted for sake of brevity. 1. Explicit equality with true (= 1 or = true):
= 0 or = false):
!= 0 or != false):
IN operator:
In the example below (1, true) is a tuple.