Documentation

The WHERE clause

Use the WHERE clause to filter data based on fields, tags, and/or timestamps.

Syntax

SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]

The WHERE clause supports conditional_expressions on fields, tags, and timestamps.

Note: InfluxDB does not support using OR in the WHERE clause to specify multiple time ranges. For example, InfluxDB returns an empty response for the following query:

SELECT * FROM "mydb" WHERE time = '2020-07-31T20:07:00Z' OR time = '2020-07-31T23:07:17Z'`

Fields

field_key <operator> ['string' | boolean | float | integer]

The WHERE clause supports comparisons against string, boolean, float, and integer field values.

Single quote string field values in the WHERE clause. Queries with unquoted string field values or double quoted string field values will not return any data and, in most cases, will not return an error.

Supported operators

Operator Meaning
= equal to
<> not equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

InfluxQL also supports Regular Expressions.

Tags

tag_key <operator> ['tag_value']

Single quote tag values in the WHERE clause. Queries with unquoted tag values or double quoted tag values will not return any data and, in most cases, will not return an error.

Supported operators

Operator Meaning
= equal to
<> not equal to
!= not equal to

Timestamps

For most SELECT statements, the default time range is between 1677-09-21 00:12:43.145224194 and 2262-04-11T23:47:16.854775806Z UTC. For SELECT statements with a GROUP BY time() clause, the default time range is between 1677-09-21 00:12:43.145224194 UTC and now().

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

Examples

Select data with specific field key-values

Select data with a specific string field key-value

Select data with a specific field key-value and perform basic arithmetic

Select data with a specific tag key-value

Select data with specific field key-values and tag key-valuest

SELECT * FROM "h2o_feet" WHERE time > now() - 7d

The query returns data from the h2o_feet measurement with timestamps within the past seven days. See Time Syntax for more in-depth information on supported time syntax in the WHERE clause.

Common issues with the WHERE clause

A WHERE clause query unexpectedly returns no data

In most cases, this issue is the result of missing single quotes around tag values or string field values. Queries with unquoted or double quoted tag values or string field values will not return any data and, in most cases, will not return an error.

The first two queries in the code block below attempt to specify the tag value santa_monica without any quotes and with double quotes. Those queries return no results. The third query single quotes santa_monica (this is the supported syntax) and returns the expected results.

SELECT "water_level" FROM "h2o_feet" WHERE "location" = santa_monica
No results

SELECT "water_level" FROM "h2o_feet" WHERE "location" = "santa_monica"
No results

SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica'

Output:

Name: h2o_feet

time water_level
2019-08-17T00:00:00Z 2.0640000000
2019-08-17T00:06:00Z 2.1160000000
2019-08-17T00:12:00Z 2.0280000000
2019-08-17T00:18:00Z 2.1260000000
2019-08-17T00:24:00Z 2.0410000000
2019-08-17T00:30:00Z 2.0510000000

The first two queries in the code block below attempt to specify the string field value at or greater than 9 feet without any quotes and with double quotes. The first query returns an error because the string field value includes white spaces. The second query returns no results. The third query single quotes at or greater than 9 feet (this is the supported syntax) and returns the expected results.

SELECT "level description" FROM "h2o_feet" WHERE "level description" = at or greater than 9 feet
ERR: 400 Bad Request: failed to parse query: found than, expected ; at line 1, char 86

SELECT "level description" FROM "h2o_feet" WHERE "level description" = "at or greater than 9 feet"
No results

SELECT "level description" FROM "h2o_feet" WHERE "level description" = 'at or greater than 9 feet'

Output:

Name: h2o_feet

time level_description
2019-08-25T04:00:00Z at or greater than 9 feet
2019-08-25T04:06:00Z at or greater than 9 feet
019-08-25T04:12:00Z at or greater than 9 feet
2019-08-25T04:18:00Z at or greater than 9 feet
2019-08-25T04:24:00Z at or greater than 9 feet

Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

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.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

InfluxDB Cloud powered by TSM