Skip to main content
The 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: An expression with NULL will otherwise never pass.

Filtering data with logical operators

You can use the following logical functions together with the WHERE 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:

Pattern matching and conditional expressions

Beyond comparison operators, you can use pattern matching and conditional expressions in the WHERE clause. See “Pattern matching and conditional expressions” for usage examples.

Expression with literals, columns or subqueries

The expression following the WHERE clause can also include literals, columns or subqueries, which are nested SELECT statements that return values used in conditions. You can mix literals, columns, and subqueries in complex conditions:

Examples

Testing for NULL

Queries with NULL values:

Filtering data with logical operators

Given the following table and data:
1. AND - both conditions must be true:
2. OR - at least one condition must be true:
3. NOT - Negates a condition:
4. XOR - Exactly one condition must be true (not both):
5. Combining multiple operators:
6. Using function syntax:
The SQL keyword syntax (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):
2. Explicit equality with false (= 0 or = false):
3. Inequality (!= 0 or != false):
4. Greater than:
5. Less than or equal:
6. Combining with other conditions:
7. Using the IN operator: In the example below (1, true) is a tuple.
You can also use an array to do this:
8. Mixing comparison styles:

Pattern matching and conditional expressions

The examples below use the table and data from the example above. Results are omitted for sake of brevity.

LIKE examples

ILIKE examples

IF examples

multiIf examples

CASE examples

Simple CASE:
Searched CASE:
Last modified on June 23, 2026