2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/operators/
2022-04-09 13:29:05 +00:00
sidebar_position: 38
sidebar_label: Operators
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Operators
2017-12-28 15:13:23 +00:00
2020-04-10 13:55:00 +00:00
ClickHouse transforms operators to their corresponding functions at the query parsing stage according to their priority, precedence, and associativity.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Access Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a[N]` – Access to an element of an array. The `arrayElement(a, N)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a.N` – Access to a tuple element. The `tupleElement(a, N)` function.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Numeric Negation Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`-a` – The `negate (a)` function.
2017-12-28 15:13:23 +00:00
2021-10-19 22:42:05 +00:00
For tuple negation: [tupleNegate ](../../sql-reference/functions/tuple-functions.md#tuplenegate ).
2022-06-02 10:55:18 +00:00
## Multiplication and Division Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a * b` – The `multiply (a, b)` function.
2017-12-28 15:13:23 +00:00
2021-10-19 22:42:05 +00:00
For multiplying tuple by number: [tupleMultiplyByNumber ](../../sql-reference/functions/tuple-functions.md#tuplemultiplybynumber ), for scalar profuct: [dotProduct ](../../sql-reference/functions/tuple-functions.md#dotproduct ).
2019-12-06 12:22:55 +00:00
`a / b` – The `divide(a, b)` function.
2017-12-28 15:13:23 +00:00
2021-10-19 22:42:05 +00:00
For dividing tuple by number: [tupleDivideByNumber ](../../sql-reference/functions/tuple-functions.md#tupledividebynumber ).
2019-12-06 12:22:55 +00:00
`a % b` – The `modulo(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Addition and Subtraction Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a + b` – The `plus(a, b)` function.
2017-12-28 15:13:23 +00:00
2021-10-19 22:42:05 +00:00
For tuple addiction: [tuplePlus ](../../sql-reference/functions/tuple-functions.md#tupleplus ).
2019-12-06 12:22:55 +00:00
`a - b` – The `minus(a, b)` function.
2017-12-28 15:13:23 +00:00
2021-10-19 22:42:05 +00:00
For tuple subtraction: [tupleMinus ](../../sql-reference/functions/tuple-functions.md#tupleminus ).
2022-06-02 10:55:18 +00:00
## Comparison Operators
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### equals function
2019-12-06 12:22:55 +00:00
`a = b` – The `equals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a == b` – The `equals(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### notEquals function
2019-12-06 12:22:55 +00:00
`a != b` – The `notEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a <> b` – The `notEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### lessOrEquals function
2019-12-06 12:22:55 +00:00
`a <= b` – The `lessOrEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### greaterOrEquals function
2019-12-06 12:22:55 +00:00
`a >= b` – The `greaterOrEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### less function
2019-12-06 12:22:55 +00:00
`a < b` – The `less(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### greater function
2019-12-06 12:22:55 +00:00
`a > b` – The `greater(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### like function
2019-12-06 12:22:55 +00:00
`a LIKE s` – The `like(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### notLike function
2019-12-06 12:22:55 +00:00
`a NOT LIKE s` – The `notLike(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### ilike function
2020-10-19 15:32:09 +00:00
`a ILIKE s` – The `ilike(a, b)` function.
2022-06-16 22:34:51 +00:00
### BETWEEN function
2019-12-06 12:22:55 +00:00
`a BETWEEN b AND c` – The same as `a >= b AND a <= c` .
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a NOT BETWEEN b AND c` – The same as `a < b OR a > c` .
2019-03-26 12:44:25 +00:00
2022-06-02 10:55:18 +00:00
## Operators for Working with Data Sets
2017-12-28 15:13:23 +00:00
2021-11-12 20:02:26 +00:00
See [IN operators ](../../sql-reference/operators/in.md ) and [EXISTS ](../../sql-reference/operators/exists.md ) operator.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### in function
2019-12-06 12:22:55 +00:00
`a IN ...` – The `in(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### notIn function
2019-12-06 12:22:55 +00:00
`a NOT IN ...` – The `notIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### globalIn function
2019-12-06 12:22:55 +00:00
`a GLOBAL IN ...` – The `globalIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### globalNotIn function
2019-12-06 12:22:55 +00:00
`a GLOBAL NOT IN ...` – The `globalNotIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2022-06-16 22:34:51 +00:00
### in subquery function
2021-10-22 08:46:44 +00:00
`a = ANY (subquery)` – The `in(a, subquery)` function.
2021-10-22 09:14:33 +00:00
2022-06-16 22:34:51 +00:00
### notIn subquery function
2021-10-26 17:09:13 +00:00
`a != ANY (subquery)` – The same as `a NOT IN (SELECT singleValueOrNull(*) FROM subquery)` .
2021-10-22 09:14:33 +00:00
2022-06-16 22:34:51 +00:00
### in subquery function
2021-10-26 17:09:13 +00:00
`a = ALL (subquery)` – The same as `a IN (SELECT singleValueOrNull(*) FROM subquery)` .
2021-10-22 09:14:33 +00:00
2022-06-16 22:34:51 +00:00
### notIn subquery function
2021-10-26 17:09:13 +00:00
`a != ALL (subquery)` – The `notIn(a, subquery)` function.
2021-10-20 17:18:14 +00:00
2021-10-22 08:46:56 +00:00
**Examples**
2021-10-20 17:18:14 +00:00
2021-10-22 08:47:22 +00:00
Query with ALL:
2021-10-20 17:18:14 +00:00
``` sql
2021-10-22 08:47:29 +00:00
SELECT number AS a FROM numbers(10) WHERE a > ALL (SELECT number FROM numbers(3, 3));
2021-10-20 17:18:14 +00:00
```
Result:
``` text
┌─a─┐
│ 6 │
│ 7 │
│ 8 │
│ 9 │
└───┘
```
2021-10-22 08:47:41 +00:00
Query with ANY:
2021-10-20 17:18:14 +00:00
``` sql
2021-10-22 08:47:48 +00:00
SELECT number AS a FROM numbers(10) WHERE a > ANY (SELECT number FROM numbers(3, 3));
2021-10-20 17:18:14 +00:00
```
Result:
``` text
┌─a─┐
│ 4 │
│ 5 │
│ 6 │
│ 7 │
│ 8 │
│ 9 │
└───┘
```
2022-06-02 10:55:18 +00:00
## Operators for Working with Dates and Times
2019-11-06 12:14:41 +00:00
2022-06-02 10:55:18 +00:00
### EXTRACT
2019-04-10 18:58:42 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 18:58:42 +00:00
EXTRACT(part FROM date);
```
2020-04-10 13:55:00 +00:00
Extract parts from a given date. For example, you can retrieve a month from a given date, or a second from a time.
2019-04-10 18:58:42 +00:00
The `part` parameter specifies which part of the date to retrieve. The following values are available:
2023-04-19 15:55:29 +00:00
- `DAY` — The day of the month. Possible values: 1– 31.
- `MONTH` — The number of a month. Possible values: 1– 12.
- `YEAR` — The year.
- `SECOND` — The second. Possible values: 0– 59.
- `MINUTE` — The minute. Possible values: 0– 59.
- `HOUR` — The hour. Possible values: 0– 23.
2019-04-10 18:58:42 +00:00
The `part` parameter is case-insensitive.
2020-05-15 04:34:54 +00:00
The `date` parameter specifies the date or the time to process. Either [Date ](../../sql-reference/data-types/date.md ) or [DateTime ](../../sql-reference/data-types/datetime.md ) type is supported.
2019-04-10 18:58:42 +00:00
Examples:
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 18:58:42 +00:00
SELECT EXTRACT(DAY FROM toDate('2017-06-15'));
SELECT EXTRACT(MONTH FROM toDate('2017-06-15'));
SELECT EXTRACT(YEAR FROM toDate('2017-06-15'));
```
2019-07-12 12:03:33 +00:00
In the following example we create a table and insert into it a value with the `DateTime` type.
2019-04-10 18:58:42 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 18:58:42 +00:00
CREATE TABLE test.Orders
(
2019-07-12 12:03:33 +00:00
OrderId UInt64,
OrderName String,
2019-04-10 18:58:42 +00:00
OrderDate DateTime
)
ENGINE = Log;
```
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 18:58:42 +00:00
INSERT INTO test.Orders VALUES (1, 'Jarlsberg Cheese', toDateTime('2008-10-11 13:23:44'));
```
2020-03-20 10:10:48 +00:00
``` sql
2019-07-12 12:03:33 +00:00
SELECT
toYear(OrderDate) AS OrderYear,
toMonth(OrderDate) AS OrderMonth,
toDayOfMonth(OrderDate) AS OrderDay,
toHour(OrderDate) AS OrderHour,
2019-04-10 18:58:42 +00:00
toMinute(OrderDate) AS OrderMinute,
toSecond(OrderDate) AS OrderSecond
FROM test.Orders;
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-10 18:58:42 +00:00
┌─OrderYear─┬─OrderMonth─┬─OrderDay─┬─OrderHour─┬─OrderMinute─┬─OrderSecond─┐
│ 2008 │ 10 │ 11 │ 13 │ 23 │ 44 │
└───────────┴────────────┴──────────┴───────────┴─────────────┴─────────────┘
```
2020-04-01 23:51:21 +00:00
You can see more examples in [tests ](https://github.com/ClickHouse/ClickHouse/blob/master/tests/queries/0_stateless/00619_extract.sql ).
2019-04-10 18:58:42 +00:00
2022-06-02 10:55:18 +00:00
### INTERVAL
2019-11-06 12:14:41 +00:00
2020-05-15 04:34:54 +00:00
Creates an [Interval ](../../sql-reference/data-types/special-data-types/interval.md )-type value that should be used in arithmetical operations with [Date ](../../sql-reference/data-types/date.md ) and [DateTime ](../../sql-reference/data-types/datetime.md )-type values.
2019-11-06 12:14:41 +00:00
Types of intervals:
- `SECOND`
- `MINUTE`
- `HOUR`
- `DAY`
- `WEEK`
- `MONTH`
- `QUARTER`
- `YEAR`
2020-11-03 22:46:52 +00:00
You can also use a string literal when setting the `INTERVAL` value. For example, `INTERVAL 1 HOUR` is identical to the `INTERVAL '1 hour'` or `INTERVAL '1' hour` .
2023-03-27 18:54:05 +00:00
:::tip
2022-04-09 13:29:05 +00:00
Intervals with different types can’ t be combined. You can’ t use expressions like `INTERVAL 4 DAY 1 HOUR` . Specify intervals in units that are smaller or equal to the smallest unit of the interval, for example, `INTERVAL 25 HOUR` . You can use consecutive operations, like in the example below.
:::
2019-11-06 12:14:41 +00:00
2020-11-03 22:46:52 +00:00
Examples:
``` sql
SELECT now() AS current_date_time, current_date_time + INTERVAL 4 DAY + INTERVAL 3 HOUR;
```
``` text
┌───current_date_time─┬─plus(plus(now(), toIntervalDay(4)), toIntervalHour(3))─┐
│ 2020-11-03 22:09:50 │ 2020-11-08 01:09:50 │
└─────────────────────┴────────────────────────────────────────────────────────┘
```
2019-11-06 12:14:41 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2020-11-03 22:46:52 +00:00
SELECT now() AS current_date_time, current_date_time + INTERVAL '4 day' + INTERVAL '3 hour';
2019-11-06 12:14:41 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-11-06 12:14:41 +00:00
┌───current_date_time─┬─plus(plus(now(), toIntervalDay(4)), toIntervalHour(3))─┐
2020-11-03 22:46:52 +00:00
│ 2020-11-03 22:12:10 │ 2020-11-08 01:12:10 │
2019-11-06 12:14:41 +00:00
└─────────────────────┴────────────────────────────────────────────────────────┘
```
2020-11-03 22:46:52 +00:00
``` sql
SELECT now() AS current_date_time, current_date_time + INTERVAL '4' day + INTERVAL '3' hour;
```
``` text
┌───current_date_time─┬─plus(plus(now(), toIntervalDay('4')), toIntervalHour('3'))─┐
│ 2020-11-03 22:33:19 │ 2020-11-08 01:33:19 │
└─────────────────────┴────────────────────────────────────────────────────────────┘
```
2021-06-14 20:06:04 +00:00
You can work with dates without using `INTERVAL` , just by adding or subtracting seconds, minutes, and hours. For example, an interval of one day can be set by adding `60*60*24` .
2021-06-12 17:46:26 +00:00
2022-04-09 13:29:05 +00:00
:::note
The `INTERVAL` syntax or `addDays` function are always preferred. Simple addition or subtraction (syntax like `now() + ...` ) doesn't consider time settings. For example, daylight saving time.
:::
2021-06-12 17:46:26 +00:00
Examples:
``` sql
2022-03-11 23:36:03 +00:00
SELECT toDateTime('2014-10-26 00:00:00', 'Asia/Istanbul') AS time, time + 60 * 60 * 24 AS time_plus_24_hours, time + toIntervalDay(1) AS time_plus_1_day;
2021-06-12 17:46:26 +00:00
```
``` text
2021-06-15 09:58:51 +00:00
┌────────────────time─┬──time_plus_24_hours─┬─────time_plus_1_day─┐
│ 2014-10-26 00:00:00 │ 2014-10-26 23:00:00 │ 2014-10-27 00:00:00 │
└─────────────────────┴─────────────────────┴─────────────────────┘
2021-06-12 17:46:26 +00:00
```
2019-11-06 12:14:41 +00:00
**See Also**
2023-04-19 15:55:29 +00:00
- [Interval ](../../sql-reference/data-types/special-data-types/interval.md ) data type
- [toInterval ](../../sql-reference/functions/type-conversion-functions.md#function-tointerval ) type conversion functions
2019-11-06 12:14:41 +00:00
2022-06-02 10:55:18 +00:00
## Logical AND Operator
2017-12-28 15:13:23 +00:00
2021-06-29 19:42:34 +00:00
Syntax `SELECT a AND b` — calculates logical conjunction of `a` and `b` with the function [and ](../../sql-reference/functions/logical-functions.md#logical-and-function ).
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Logical OR Operator
2017-12-28 15:13:23 +00:00
2021-06-29 19:42:34 +00:00
Syntax `SELECT a OR b` — calculates logical disjunction of `a` and `b` with the function [or ](../../sql-reference/functions/logical-functions.md#logical-or-function ).
2021-06-28 18:25:52 +00:00
2022-06-02 10:55:18 +00:00
## Logical Negation Operator
2021-06-28 19:32:37 +00:00
2021-06-29 19:42:34 +00:00
Syntax `SELECT NOT a` — calculates logical negation of `a` with the function [not ](../../sql-reference/functions/logical-functions.md#logical-not-function ).
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Conditional Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a ? b : c` – The `if(a, b, c)` function.
2017-12-28 15:13:23 +00:00
Note:
2020-05-15 04:34:54 +00:00
The conditional operator calculates the values of b and c, then checks whether condition a is met, and then returns the corresponding value. If `b` or `C` is an [arrayJoin() ](../../sql-reference/functions/array-join.md#functions_arrayjoin ) function, each row will be replicated regardless of the “a” condition.
2018-09-04 11:18:59 +00:00
2022-06-02 10:55:18 +00:00
## Conditional Expression
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
CASE [x]
WHEN a THEN b
[WHEN ... THEN ...]
2018-11-01 13:28:45 +00:00
[ELSE c]
2017-12-28 15:13:23 +00:00
END
```
2018-11-01 13:28:45 +00:00
If `x` is specified, then `transform(x, [a, ...], [b, ...], c)` function is used. Otherwise – `multiIf(a, b, ..., c)` .
If there is no `ELSE c` clause in the expression, the default value is `NULL` .
The `transform` function does not work with `NULL` .
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Concatenation Operator
2017-12-28 15:13:23 +00:00
`s1 || s2` – The `concat(s1, s2) function.`
2022-06-02 10:55:18 +00:00
## Lambda Creation Operator
2017-12-28 15:13:23 +00:00
`x -> expr` – The `lambda(x, expr) function.`
2020-04-10 13:55:00 +00:00
The following operators do not have a priority since they are brackets:
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Array Creation Operator
2017-12-28 15:13:23 +00:00
`[x1, ...]` – The `array(x1, ...) function.`
2022-06-02 10:55:18 +00:00
## Tuple Creation Operator
2017-12-28 15:13:23 +00:00
`(x1, x2, ...)` – The `tuple(x2, x2, ...) function.`
2022-06-02 10:55:18 +00:00
## Associativity
2017-12-28 15:13:23 +00:00
All binary operators have left associativity. For example, `1 + 2 + 3` is transformed to `plus(plus(1, 2), 3)` .
2021-05-27 19:44:11 +00:00
Sometimes this does not work the way you expect. For example, `SELECT 4 > 2 > 3` will result in 0.
2017-12-28 15:13:23 +00:00
2020-04-10 13:55:00 +00:00
For efficiency, the `and` and `or` functions accept any number of arguments. The corresponding chains of `AND` and `OR` operators are transformed into a single call of these functions.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## Checking for `NULL`
2018-09-04 11:18:59 +00:00
ClickHouse supports the `IS NULL` and `IS NOT NULL` operators.
2022-06-02 10:55:18 +00:00
### IS NULL
2018-09-04 11:18:59 +00:00
2023-04-19 15:55:29 +00:00
- For [Nullable ](../../sql-reference/data-types/nullable.md ) type values, the `IS NULL` operator returns:
- `1` , if the value is `NULL` .
- `0` otherwise.
- For other values, the `IS NULL` operator always returns `0` .
2018-09-04 11:18:59 +00:00
2021-06-29 13:27:54 +00:00
Can be optimized by enabling the [optimize_functions_to_subcolumns ](../../operations/settings/settings.md#optimize-functions-to-subcolumns ) setting. With `optimize_functions_to_subcolumns = 1` the function reads only [null ](../../sql-reference/data-types/nullable.md#finding-null ) subcolumn instead of reading and processing the whole column data. The query `SELECT n IS NULL FROM table` transforms to `SELECT n.null FROM TABLE` .
2021-06-23 00:15:11 +00:00
2020-03-20 10:10:48 +00:00
<!-- -->
``` sql
2019-09-23 15:31:46 +00:00
SELECT x+100 FROM t_null WHERE y IS NULL
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─plus(x, 100)─┐
│ 101 │
└──────────────┘
```
2022-06-02 10:55:18 +00:00
### IS NOT NULL
2018-09-04 11:18:59 +00:00
2023-04-19 15:55:29 +00:00
- For [Nullable ](../../sql-reference/data-types/nullable.md ) type values, the `IS NOT NULL` operator returns:
- `0` , if the value is `NULL` .
- `1` otherwise.
- For other values, the `IS NOT NULL` operator always returns `1` .
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
<!-- -->
``` sql
2019-09-23 15:31:46 +00:00
SELECT * FROM t_null WHERE y IS NOT NULL
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┬─y─┐
│ 2 │ 3 │
└───┴───┘
```
2021-06-23 00:15:11 +00:00
2021-06-29 13:27:54 +00:00
Can be optimized by enabling the [optimize_functions_to_subcolumns ](../../operations/settings/settings.md#optimize-functions-to-subcolumns ) setting. With `optimize_functions_to_subcolumns = 1` the function reads only [null ](../../sql-reference/data-types/nullable.md#finding-null ) subcolumn instead of reading and processing the whole column data. The query `SELECT n IS NOT NULL FROM table` transforms to `SELECT NOT n.null FROM TABLE` .