WHERE keyword
WHERE
clause filters data. Filter expressions are required to return boolean
result.
info
QuestDB includes a JIT compiler for SQL queries which contain WHERE
clauses.
To find out more about this functionality with details on enabling its use, see
the JIT compiler documentation.
#
SyntaxThe general syntax is as follows. Specific filters have distinct syntaxes detailed thereafter.
#
Logical operatorsQuestDB supports AND
, OR
, NOT
as logical operators and can assemble
conditions using brackets ()
.
#
Symbol and stringQuestDB can filter strings and symbols based on equality, inequality, and regular expression patterns.
#
Exact matchEvaluates match of a string or symbol.
name | age |
---|---|
John | 31 |
John | 45 |
... | ... |
#
Does NOT matchEvaluates mismatch of a string or symbol.
name | age |
---|---|
Tim | 31 |
Tom | 45 |
... | ... |
#
Regular expression matchEvaluates match against a regular expression defined using java.util.regex patterns.
name | age |
---|---|
Joe | 31 |
Jonathan | 45 |
... | ... |
#
Regular expression does NOT matchEvaluates mismatch against a regular expression defined using java.util.regex patterns.
name | age |
---|---|
Tim | 31 |
Tom | 45 |
... | ... |
#
List searchEvaluates match or mismatch against a list of elements.
name | age |
---|---|
Tim | 31 |
Tom | 45 |
... | ... |
name | age |
---|---|
Aaron | 31 |
Amelie | 45 |
... | ... |
#
NumericQuestDB can filter numeric values based on equality, inequality, comparison, and proximity
note
For timestamp filters, we recommend the timestamp search notation which is faster and less verbose.
#
Equality, inequality and comparison#
ProximityEvaluates whether the column value is within a range of the target value. This
is useful to simulate equality on double
and float
values.
tip
When performing multiple equality checks of double values against integer constants, it may be preferable to store double values as long integers with a scaling factor.
#
BooleanUsing the columnName will return true
values. To return false
values,
precede the column name with the NOT
operator.
userId | isActive |
---|---|
12532 | true |
38572 | true |
... | ... |
userId | isActive |
---|---|
876534 | false |
43234 | false |
... | ... |
#
Timestamp and dateQuestDB supports both its own timestamp search notation and standard search based on inequality. This section describes the use of the timestamp search notation which is efficient and fast but requires a designated timestamp.
If a table does not have a designated timestamp applied during table creation, one may be applied dynamically during a select operation.
#
Native timestamp formatQuestDB automatically recognizes strings formatted as ISO timestamp as a
timestamp
type. The following are valid examples of strings parsed as
timestamp
types:
Valid STRING Format | Resulting Timestamp |
---|---|
2010-01-12T12:35:26.123456+01:30 | 2010-01-12T11:05:26.123456Z |
2010-01-12T12:35:26.123456+01 | 2010-01-12T11:35:26.123456Z |
2010-01-12T12:35:26.123456Z | 2010-01-12T12:35:26.123456Z |
2010-01-12T12:35:26.12345 | 2010-01-12T12:35:26.123450Z |
2010-01-12T12:35:26.1234 | 2010-01-12T12:35:26.123400Z |
2010-01-12T12:35:26.123 | 2010-01-12T12:35:26.123000Z |
2010-01-12T12:35:26.12 | 2010-01-12T12:35:26.120000Z |
2010-01-12T12:35:26.1 | 2010-01-12T12:35:26.100000Z |
2010-01-12T12:35:26 | 2010-01-12T12:35:26.000000Z |
2010-01-12T12:35 | 2010-01-12T12:35:00.000000Z |
2010-01-12T12 | 2010-01-12T12:00:00.000000Z |
2010-01-12 | 2010-01-12T00:00:00.000000Z |
2010-01 | 2010-01-01T00:00:00.000000Z |
2010 | 2010-01-01T00:00:00.000000Z |
2010-01-12 12:35:26.123456-02:00 | 2010-01-12T14:35:26.123456Z |
2010-01-12 12:35:26.123456Z | 2010-01-12T12:35:26.123456Z |
2010-01-12 12:35:26.123 | 2010-01-12T12:35:26.123000Z |
2010-01-12 12:35:26.12 | 2010-01-12T12:35:26.120000Z |
2010-01-12 12:35:26.1 | 2010-01-12T12:35:26.100000Z |
2010-01-12 12:35:26 | 2010-01-12T12:35:26.000000Z |
2010-01-12 12:35 | 2010-01-12T12:35:00.000000Z |
#
Exact timestamp#
Syntaxts | score |
---|---|
2010-01-12T00:02:26.000Z | 2.4 |
2010-01-12T00:02:26.000Z | 3.1 |
... | ... |
ts | score |
---|---|
2010-01-12T00:02:26.000000Z | 2.4 |
2010-01-12T00:02:26.000000Z | 3.1 |
... | ... |
#
Time rangeReturn results within a defined range
#
Syntaxts | score |
---|---|
2018-01-01T00:0000.000000Z | 123.4 |
... | ... |
2018-12-31T23:59:59.999999Z | 115.8 |
ts | score |
---|---|
2018-05-23T12:15:00.000000Z | 123.4 |
... | ... |
2018-05-23T12:15:59.999999Z | 115.8 |
#
Time range with modifierYou can apply a modifier to further customize the range. The algorithm will calculate the resulting range by modifying the upper bound of the original range by the modifier parameter. An optional occurrence can be set to apply the time range repeatedly for a set number of times.
#
Syntaxperiod
is an unsigned integer.interval
is an unsigned integer.repetition
is an unsigned integer.
#
ExamplesModifying the range:
The range is 2018. The modifier extends the upper bound (originally 31 Dec 2018) by one month.
ts | score |
---|---|
2018-01-01T00:00:00.000000Z | 123.4 |
... | ... |
2019-01-31T23:59:59.999999Z | 115.8 |
Modifying the interval:
The range is Jan 1 2018 with a one-year interval and the total occurrence is two.
ts | score |
---|---|
2018-01-01T00:00:00.000000Z | 123.4 |
... | ... |
2018-01-01T23:59:59.999999Z | 113.8 |
2019-01-01T00:00:00.000000Z | 125.7 |
... | ... |
2019-01-01T23:59:59.999999Z | 103.9 |
#
IN with multiple arguments#
SyntaxIN
with more than 1 argument is treated as standard SQL IN
. It is a
shorthand of multiple OR
conditions, i.e. the following query:
is equivalent to:
ts | value |
---|---|
2018-01-01T00:00:00.000000Z | 123.4 |
2018-01-01T12:00:00.000000Z | 589.1 |
2018-01-02T00:00:00.000000Z | 131.5 |
#
BETWEEN#
SyntaxFor non-standard ranges, users can explicitly specify the target range using the
BETWEEN
operator. As with standard SQL, both upper and lower bounds of
BETWEEN
are inclusive, and the order of lower and upper bounds is not
important so that BETWEEN X AND Y
is equivalent to BETWEEN Y AND X
.
ts | value |
---|---|
2018-01-01T00:00:23.000000Z | 123.4 |
... | ... |
2018-01-01T00:00:23.500000Z | 131.5 |
BETWEEN
can accept non-constant bounds, for example, the following query will
return all records older than one year before the current date: