Operators
Describes the CQL operators.
Arithmetic operators
- -
- Use the minus symbol for either the unary negate operand or subtraction.
- +
- Use the plus symbol for addition.
- *
- Use the asterisk symbol for multiplication.
- /
- Use the forward slash symbol for division.
- %
- Use the percent symbol to return the remainder from division.
The *, /, and % operators have a higher precedence than the + and - operators. Higher precedence operators are evaluated before lower precedence operators. If two operators in an expression have the same precedence, they are evaluated from left to right based on the operator position in the expression.
Return types
The return type of an arithmetic expression is based on the types of the input operands. In the following table, left and right refer to the type of the operand on the left and right of the operator. For example, in the expression a + b, a is the left operand and b is the right operand; if a is an int operand and b is a float operand, a float is returned.
right | tinyint | smallint | int | bigint | counter | float | double | varint | decimal | |
---|---|---|---|---|---|---|---|---|---|---|
left | ||||||||||
tinyint | tinyint | smallint | int | bigint | bigint | float | double | varint | decimal | |
smallint | smallint | smallint | int | bigint | bigint | float | double | varint | decimal | |
int | int | int | int | bigint | bigint | float | double | varint | decimal | |
bigint | bigint | bigint | bigint | bigint | bigint | double | double | varint | decimal | |
counter | bigint | bigint | bigint | bigint | bigint | double | double | varint | decimal | |
float | float | float | float | double | double | float | double | decimal | decimal | |
double | double | double | double | double | double | double | double | decimal | decimal | |
varint | varint | varint | varint | decimal | decimal | decimal | decimal | decimal | decimal | |
decimal | decimal | decimal | decimal | decimal | decimal | decimal | decimal | decimal | decimal |
Dates and times
A time duration can be added (+) or subtracted (-) from a timestamp or a date to create a new timestamp or date. For example, the following query returns cycling races that occur one day before the supplied date:
SELECT * FROM cycling.race_times WHERE race_date = '2017-04-15' - 1d;
Multiple columns
Multiple columns can be used in an expression, as shown in the following queries:
SELECT (emp_id + dept_id) / age FROM cycling.mechanic; SELECT emp_id + dept_id / age FROM cycling.mechanic;