Documentation

WHERE clause

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

Syntax

SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]
  • Copy
  • Fill window
  • 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.

WHERE
  time >= '2023-01-01T00:00:00Z'
  AND time < '2023-07-01T00:00:00Z'
  • Copy
  • Fill window
WHERE
  time >= 1672531200000000000
  AND time < 1688169600000000000
  • Copy
  • Fill window
WHERE
  time >= now() - 1d
  AND time < now()
  • Copy
  • Fill window

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

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
SELECT * FROM home WHERE room =~ /^K/
  • Copy
  • Fill window

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

WHERE clause examples

The following examples use the Home sensor sample dataset.

Select data with a specific tag value

Select data from a specific time range

Select data from a relative time range

Select field values above a threshold

Select specific field values

Select field values based on arithmetic

Select data with field values above a threshold and a specific tag value

Select data based on the relationship between columns

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:

"location" = 'London'
  • Copy
  • Fill window

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

"location" = "London"
  • Copy
  • Fill window

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:

SELECT *
FROM home
WHERE
  (time >= '2025-03-25T08:00:00Z' AND time <= '2025-03-25T10:00:00Z')
  OR (time >= '2025-03-25T18:00:00Z' AND time <= '2025-03-25T20:00:00Z')
  • Copy
  • Fill window

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out:

InfluxDB Cloud Serverless