Skip to main content
Operators are represented by special characters or keywords, they do not use function call syntax.

Operator Precedence

The following table lists all SQL operators from highest to lowest precedence, alongside with their associativity. The precedence and associativity of the operators is hard-coded into the parser. Add parentheses if you want an expression with multiple operators to be interpreted in some other way than what the precedence rules imply.

:: Type cast

Use can use the :: operator instead of the CAST function to convert one data type to another.

Syntax

Example

Arithmetic with numbers

In arithmetic operations like +, -, *, and / , the result’s data type aligns with the most encompassing type of the operands indicated as “Corresponding type” in the table above. For clarity:
  • When both operands are of the same data type (e.g., two INTEGERs or two NUMERICs), the result will also be of that same data type.
    • INTEGER <op> INTEGER = INTEGER
    • INTEGER <op> BIGINT = BIGINT
  • For operations involving two different numeric data types, the result will typically be of the more precise or larger data type.
    • INTEGER <op> REAL = DOUBLE PRECISION
  • Overflow checks and floating point errors are applied according to the result data type only.
Floating point precision means that the representation of a number is accurate up to a certain number of digits. In Firebolt, REAL data types have 6-digit precision and DOUBLE PRECISION have 16-digit precision. This means that calculations have a precision of 6 or 16 respectively, and numbers are truncated to that precision. For example, if a number is stored as 1.234567, it is automatically truncated to 1.23456 for REAL.

When performing arithmetic, the number of leading digits in the output is the product of the leading digits in both inputs. This means that if either or both of the input numbers are larger than 6, those numbers are the first truncated, and then the arithmetic is performed.

Arithmetic with date/time and intervals

An interval represents a duration. In Firebolt, values of type interval can be used to add or subtract a duration to/from a date or timestamp. Interval cannot be used as the data type of a column. The + and * operators shown above come in commutative pairs (e.g., both DATE + interval and interval + DATE are accepted).

Literal string interpretation

Interval literals can be specified in two formats.

Format examples

Cast from text literal
The following examples demonstrate different ways to cast a text literal to an interval in SQL. Each method converts a string representing a time interval into the INTERVAL data type using various casting syntaxes.
Using interval keyword:
Using double colon:
Using CAST function:
In these examples, direction can be either ago or left empty. Using ago negates all specified quantities. The quantity represents a signed or unsigned integer, and unit refers to one of the following time units, matched case-insensitively: Each unit can appear only once in an interval literal. The value of the interval is determined by adding the quantities of the specified units with the appropriate signs. Unit outside of text literal
The following example demonstrates how to cast a numeric value to an interval by placing the unit outside of the text literal.
In this format, N represents a signed or unsigned integer, and unit specifies the time unit, matched case-insensitively, from the following options:

Arithmetic between interval and TIMESTAMPTZ

Interval arithmetic with TIMESTAMPTZ values works as follows:
  1. Convert the TIMESTAMPTZ value from Unix time to local time according to the rules of the time zone specified by the session’s time_zone setting.
  2. Add the millennium, century, decade, year, month, week and day components of the interval to the local time.
  3. Convert the local time back to Unix time according to the rules of the time zone specified by the session’s time_zone setting.
  4. Add the hour, minute, second, millisecond, and microsecond components of the interval to the Unix time.
The back and forth between Unix time and local time is necessary to handle the fact that not all days consist of 24 hours due to daylight savings time transitions. For instance, SELECT TIMESTAMPTZ '2022-10-30 Europe/Berlin' + interval '1 day' returns 2022-10-31 00:00:00+01 but SELECT TIMESTAMPTZ '2022-10-30 Europe/Berlin' + interval '24 hours' returns 2022-10-30 23:00:00+01 (assuming the value of the session’s time_zone setting is 'Europe/Berlin'). Still, the dependence on the session’s time_zone setting should be kept in mind when doing arithmetic between interval and TIMESTAMPTZ.

Multiplying an interval by a scalar

You can use the expression date_time + INTERVAL * d where date_time is a constant or column reference of type DATE, TIMESTAMP, or TIMESTAMPTZ, and d is a constant or column reference of type DOUBLE PRECISION. The effect is that the INTERVAL is scaled by d, and the resulting INTERVAL is added to date_time. E.g., INTERVAL '1 day' * 3 -> INTERVAL '3 days'.

Examples

Rows: 1Execution time: 5ms

Rows: 1Execution time: 5ms

Rows: 1Execution time: 5ms

Rows: 1Execution time: 5ms

The following example shows a daylight savings time change in the time zone 'Europe/Berlin':

Logical

Logical operators return the result of a boolean operation using three valued logic

Comparison

Example of using comparison operator in WHERE clause

CASE

Conditional expression similar to if-then-else statements. If the result of the condition is true, then the value of the CASE expression is the result that follows the condition. If the result is false, any subsequent WHEN clauses (conditions) are searched in the same manner. If no WHEN condition is true, then the value of the case expression is the result specified in the ELSE clause. If the ELSE clause is omitted and no condition matches, the result is NULL.

Syntax

Parameters

Return type

Same data type as <result>

Example

This example references a table player_level with the following columns and values: The following example categorizes each entry by length. If the movie is longer than zero minutes and less than 50 minutes it is categorized as SHORT. When the length is 50-120 minutes, it’s categorized as Medium, and when even longer, it’s categorized as Long.
Returns:

String

To concatenate strings, you can use the CONCAT function.

Rows: 1Execution time: 5ms

Alternatively, you can use the double pipe || operator.

Rows: 1Execution time: 5ms

Subquery operators

Subqueries are queries nested within another query. They allow complex data retrieval by enabling a query to filter results based on the outcome of another query. Subquery operators are crucial in constructing these nested queries, especially within the WHERE clause, to filter data based on specific conditions.

Example–using EXISTS to find all suppliers with products equal to the price of 22

Example–using the IN operator to return all customers from Mannheim or London

Example–using a correlated subquery to retrieve all the products that cost more than the average price

Example–using a scalar boolean subquery to retrieve rows based on true/false condition