Use a WHERE clause to filter data by field values, tag values, or timestamps.
Syntax
SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]WHERE clauses accept conditional expressions on fields, tags, and timestamps, combined with AND and OR operators. Use parentheses to control operator precedence.
Fields
field_key <operator> ['string' | boolean | float | integer]Field values can be strings, Booleans, floats, or integers.
Always enclose string field values in single quotation marks ('). Queries with unquoted or double-quoted string values return no data and no error. See No results returned for examples.
Supported operators:
| Operator | Description |
|---|---|
= | Equal to |
<> | Not equal to |
!= | Not equal to |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
Arithmetic operators and regular expressions are also supported.
Tags
tag_key <operator> ['tag_value']Always enclose tag values in single quotation marks ('). Queries with unquoted or double-quoted tag values return no data and no error. See No results returned for examples.
Supported operators:
| Operator | Description |
|---|---|
= | Equal to |
<> | Not equal to |
!= | Not equal to |
Regular expressions are also supported.
Timestamps
The default time range depends on the query type:
| Query type | Default time range |
|---|---|
| Most SELECT statements | 1677-09-21 00:12:43.145224194 UTC to 2262-04-11T23:47:16.854775806Z UTC |
| SELECT with GROUP BY time() | 1677-09-21 00:12:43.145224194 UTC to now() |
For the full timestamp syntax, see the Time syntax topic.
Examples
Filter by a numeric field value
SELECT * FROM "h2o_feet" WHERE "water_level" > 8Returns all data points in h2o_feet where water_level exceeds 8:
| time | level description | location | water_level |
|---|---|---|---|
| 2015-08-18T00:00:00Z | between 6 and 9 feet | coyote_creek | 8.12 |
| 2015-08-18T00:06:00Z | between 6 and 9 feet | coyote_creek | 8.005 |
| [...] | |||
| 2015-09-18T00:12:00Z | between 6 and 9 feet | coyote_creek | 8.189 |
| 2015-09-18T00:18:00Z | between 6 and 9 feet | coyote_creek | 8.084 |
Filter by a string field value
SELECT * FROM "h2o_feet" WHERE "level description" = 'below 3 feet'Returns all data points where level description equals below 3 feet. String field values must be in single quotation marks.
| time | level description | location | water_level |
|---|---|---|---|
| 2015-08-18T00:00:00Z | below 3 feet | santa_monica | 2.064 |
| 2015-08-18T00:06:00Z | below 3 feet | santa_monica | 2.116 |
| [...] | |||
| 2015-09-18T14:06:00Z | below 3 feet | santa_monica | 2.999 |
| 2015-09-18T14:36:00Z | below 3 feet | santa_monica | 2.907 |
Filter using an arithmetic operator
SELECT * FROM "h2o_feet" WHERE "water_level" + 2 > 11.9Returns data points where adding 2 to water_level exceeds 11.9. Standard arithmetic operator precedence applies. For details, see the InfluxQL arithmetic operators topic.
| time | level description | location | water_level |
|---|---|---|---|
| 2015-08-29T07:06:00Z | at or greater than 9 feet | coyote_creek | 9.902 |
| 2015-08-29T07:12:00Z | at or greater than 9 feet | coyote_creek | 9.938 |
| 2015-08-29T07:18:00Z | at or greater than 9 feet | coyote_creek | 9.957 |
| 2015-08-29T07:24:00Z | at or greater than 9 feet | coyote_creek | 9.964 |
| 2015-08-29T07:30:00Z | at or greater than 9 feet | coyote_creek | 9.954 |
| 2015-08-29T07:36:00Z | at or greater than 9 feet | coyote_creek | 9.941 |
| 2015-08-29T07:42:00Z | at or greater than 9 feet | coyote_creek | 9.925 |
| 2015-08-29T07:48:00Z | at or greater than 9 feet | coyote_creek | 9.902 |
| 2015-09-02T23:30:00Z | at or greater than 9 feet | coyote_creek | 9.902 |
Filter by a tag value
SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica'Returns all data points where the location tag equals santa_monica. Tag values must be in single quotation marks.
| time | water_level |
|---|---|
| 2015-08-18T00:00:00Z | 2.064 |
| 2015-08-18T00:06:00Z | 2.116 |
| [...] | |
| 2015-09-18T21:36:00Z | 5.066 |
| 2015-09-18T21:42:00Z | 4.938 |
Filter by field and tag values together
SELECT "water_level" FROM "h2o_feet" WHERE "location" <> 'santa_monica' AND ("water_level" < -0.59 OR "water_level" > 9.95)Returns data points where location is not santa_monica and water_level is below -0.59 or above 9.95. Use parentheses to group OR conditions within an AND expression.
| time | water_level |
|---|---|
| 2015-08-29T07:18:00Z | 9.957 |
| 2015-08-29T07:24:00Z | 9.964 |
| 2015-08-29T07:30:00Z | 9.954 |
| 2015-08-29T14:30:00Z | -0.61 |
| 2015-08-29T14:36:00Z | -0.591 |
| 2015-08-30T15:18:00Z | -0.594 |
Filter by timestamp
SELECT * FROM "h2o_feet" WHERE time > now() - 7dReturns data points from h2o_feet with timestamps in the last seven days. For the full timestamp syntax, see the Time syntax topic.
FAQ
No results returned
If a query returns no results and no error, check that all string field values and tag values are enclosed in single quotation marks (').
Tag values:
SELECT "water_level" FROM "h2o_feet" WHERE "location" = santa_monica
SELECT "water_level" FROM "h2o_feet" WHERE "location" = "santa_monica"
SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica'The first two queries return no data. The third, with single quotation marks, returns the expected results:
| time | water_level |
|---|---|
| 2015-08-18T00:00:00Z | 2.064 |
| [...] | |
| 2015-09-18T21:42:00Z | 4.938 |
String field values:
SELECT "level description" FROM "h2o_feet" WHERE "level description" = at or greater than 9 feet
ERR: error parsing query: found than, expected ; at line 1, char 86
SELECT "level description" FROM "h2o_feet" WHERE "level description" = "at or greater than 9 feet"
SELECT "level description" FROM "h2o_feet" WHERE "level description" = 'at or greater than 9 feet'Without quotes, a parse error is returned because the value contains spaces. With double quotation marks, the query runs but returns no data. With single quotation marks, the query returns the expected results:
| time | level description |
|---|---|
| 2015-08-26T04:00:00Z | at or greater than 9 feet |
| [...] | |
| 2015-09-15T22:42:00Z | at or greater than 9 feet |