Back to Influxdb

Where

content/shared/influxql-v3-reference/where.md

latest10.6 KB
Original Source

Use the WHERE clause to filter data based on field values, tag values, and timestamps.

Syntax

sql
SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]
  • conditional_expression: Comparison between two operands that evaluates to true or false. Comparison logic is determined by operators used in the expression. These expressions can operate on InfluxDB fields, tags, and timestamps. Use logical operators (AND, OR) to chain multiple conditional expressions together.

Operators

Operators evaluate the relationship between two operands and return true or false.

Comparison operators

OperatorMeaningSupported data types
=Equal toall
<>Not equal toall
!=Not equal toall
>Greater thannumeric, timestamp
>=Greater than or equal tonumeric, timestamp
<Less thannumeric, timestamp
<=Less than or equal tonumeric, timestamp
=~Matches a regular expressionstrings
!~Doesn't match a regular expressionstrings

Logical operators

OperatorMeaning
ANDReturns true if both operands are true. Otherwise, returns false.
ORReturns true if any operand is true. Otherwise, returns false.

Time ranges

Use the WHERE clause to specify a time range to query. If a time range isn't specified in the WHERE clause, the default time range is used.

Timestamps are stored in the time column. Use comparison operators to compare the value of the time column to a timestamp literal, integer (Unix nanosecond timestamp), or expression.

{{< code-tabs-wrapper >}} {{% code-tabs %}} Timestamp Integer Expression {{% /code-tabs %}} {{% code-tab-content %}}

sql
WHERE
  time >= '2023-01-01T00:00:00Z'
  AND time < '2023-07-01T00:00:00Z'

{{% /code-tab-content %}} {{% code-tab-content %}}

sql
WHERE
  time >= 1672531200000000000
  AND time < 1688169600000000000

{{% /code-tab-content %}} {{% code-tab-content %}}

sql
WHERE
  time >= now() - 1d
  AND time < now()

{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}

See Time syntax for information on how to specify alternative time ranges in the WHERE clause.

[!Important] InfluxQL does not support querying multiple time ranges.

Regular expressions

Regular expressions can be used to evaluate string values in the WHERE clause using regular expression comparison operators:

  • =~: Matches a regular expression
  • !~: Doesn't match a regular expression
sql
SELECT * FROM home WHERE room =~ /^K/

For more information about InfluxQL regular expression syntax, see InfluxQL regular expressions.

WHERE clause examples

The following examples use the Home sensor sample dataset.

{{< expand-wrapper >}} {{% expand "Select data with a specific tag value" %}}

sql
SELECT * FROM home WHERE room = 'Living Room'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timecohumroomtemp
2022-01-01T08:00:00Z035.9Living Room21.1
2022-01-01T09:00:00Z035.9Living Room21.4
2022-01-01T10:00:00Z036Living Room21.8
2022-01-01T11:00:00Z036Living Room22.2
2022-01-01T12:00:00Z035.9Living Room22.2
...............

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select data from a specific time range" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT *
FROM home
WHERE
  time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T10:00:00Z'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecohumroomtemp
2022-01-01T08:00:00Z035.9Kitchen21
2022-01-01T08:00:00Z035.9Living Room21.1
2022-01-01T09:00:00Z036.2Kitchen23
2022-01-01T09:00:00Z035.9Living Room21.4
2022-01-01T10:00:00Z036.1Kitchen22.7
2022-01-01T10:00:00Z036Living Room21.8

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select data from a relative time range" %}}

{{% influxdb/custom-timestamps %}}

sql
SELECT * FROM home WHERE time >= '2022-01-01T20:00:00Z' - 2h

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

timecohumroomtemp
2022-01-01T18:00:00Z1836.9Kitchen23.3
2022-01-01T18:00:00Z936.2Living Room22.8
2022-01-01T19:00:00Z2236.6Kitchen23.1
2022-01-01T19:00:00Z1436.3Living Room22.5
2022-01-01T20:00:00Z2636.5Kitchen22.7
2022-01-01T20:00:00Z1736.4Living Room22.2

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select field values above a threshold" %}}

sql
SELECT co FROM home WHERE co > 9

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timeco
2022-01-01T18:00:00Z18
2022-01-01T19:00:00Z14
2022-01-01T19:00:00Z22
2022-01-01T20:00:00Z17
2022-01-01T20:00:00Z26

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select specific field values" %}}

sql
SELECT room, co FROM home WHERE co = 9

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timeroomco
2022-01-01T17:00:00ZKitchen9
2022-01-01T18:00:00ZLiving Room9

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select field values based on arithmetic" %}}

sql
SELECT room, co FROM home WHERE co - 10 > 5

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timeroomco
2022-01-01T18:00:00ZKitchen18
2022-01-01T19:00:00ZKitchen22
2022-01-01T20:00:00ZLiving Room17
2022-01-01T20:00:00ZKitchen26

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select data with field values above a threshold and a specific tag value" %}}

sql
SELECT * FROM home WHERE temp > 22.7 AND room = 'Kitchen'

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timecohumroomtemp
2022-01-01T09:00:00Z036.2Kitchen23
2022-01-01T13:00:00Z136.5Kitchen22.8
2022-01-01T14:00:00Z136.3Kitchen22.8
2022-01-01T18:00:00Z1836.9Kitchen23.3
2022-01-01T19:00:00Z2236.6Kitchen23.1

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "Select data based on the relationship between columns" %}}

sql
SELECT co, temp FROM home WHERE co > temp

{{% influxql/table-meta %}} name: home {{% /influxql/table-meta %}}

{{% influxdb/custom-timestamps %}}

timecotemp
2022-01-01T20:00:00Z2622.7

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

Notable behaviors

Single and double quotes

In InfluxQL, single quotation marks (') and double quotation marks (") work differently and can alter the way a WHERE clause functions. Single quotes are used in string and timestamp literals. Double quotes are used to quote identifiers, (time, field, and tag column names).

For example, the following conditional expression compares the value of the location column to the literal string, London:

sql
"location" = 'London'

The following conditional expression compares the value of the location column to the value of the London column:

sql
"location" = "London"

Misused double and single quotes in the WHERE clause often results in unexpected empty query results. For more information about quotation marks, see InfluxQL quotation.

Cannot query multiple time ranges

InfluxDB does not support using OR in the WHERE clause to query multiple time ranges. For example, the following query returns no results:

{{% influxdb/custom-timestamps %}}

sql
SELECT *
FROM home
WHERE
  (time >= '2022-01-01T08:00:00Z' AND time <= '2022-01-01T10:00:00Z')
  OR (time >= '2022-01-01T18:00:00Z' AND time <= '2022-01-01T20:00:00Z')

{{% /influxdb/custom-timestamps %}}