Get started querying data

  • 3 / 5

InfluxDB supports many different tools for querying data, including:

This tutorial walks you through the fundamentals of querying data in InfluxDB and focuses primarily on the two languages you can use to query your time series data:

  • Flux: A functional scripting language designed to query and process data from InfluxDB and other data sources.
  • InfluxQL: A SQL-like query language designed to query time series data from InfluxDB.

The examples in this section of the tutorial query the data from written in the Get started writing data section.

On this page:

Query data with Flux

Flux is a functional scripting language that lets you query and process data from InfluxDB and other data sources.

This is a brief introduction to writing Flux queries. For a more in-depth introduction, see Get started with Flux.

Flux query basics

When querying InfluxDB with Flux, there are three primary functions you use:

  • from(): Queries data from an InfluxDB bucket.

  • range(): Filters data based on time bounds. Flux requires “bounded” queries—queries limited to a specific time range.

  • filter(): Filters data based on column values. Each row is represented by r and each column is represented by a property of r. You can apply multiple subsequent filters.

    To see how from() structures data into rows and tables when returned from InfluxDB, view the data written in Get started writing to InfluxDB.

    Learn more about how filter() works

    filter() reads each row as a record named r. In the r record, each key-value pair represents a column and its value. For example:

    1. r = {
    2. _time: 2020-01-01T00:00:00Z,
    3. _measurement: "home",
    4. room: "Kitchen",
    5. _field: "temp",
    6. _value: 21.0,
    7. }

    To filter rows, use predicate expressions to evaluate the values of columns. Given the row record above:

    1. (r) => r._measurement == "home" // Returns true
    2. (r) => r.room == "Kitchen" // Returns true
    3. (r) => r._field == "co" // Returns false
    4. (r) => r._field == "co" or r._field == "temp" // Returns true
    5. (r) => r._value <= 20.0 // Returns false

    Rows that evaluate to true are included in the filter() output. Rows that evaluate to false are dropped from the filter() output.

Pipe-forward operator

Flux uses the pipe-forward operator (|>) to pipe the output of one function as input the next function as input.

Query the example data

The following Flux query returns the co, hum, and temp fields stored in the home measurement with timestamps between 2022-01-01T08:00:00Z and 2022-01-01T20:00:01Z.

  1. from(bucket: "get-started")
  2. |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
  3. |> filter(fn: (r) => r._measurement == "home")
  4. |> filter(fn: (r) => r._field== "co" or r._field == "hum" or r._field == "temp")

Execute a Flux query

Use the InfluxDB UI, influx CLI, or InfluxDB API to execute Flux queries.

InfluxDB UI influx CLI InfluxDB API

  1. Visit localhost:8086 in a browser to log in and access the InfluxDB UI.

  2. In the left navigation bar, click Data Explorer.

Data Explorer

  1. The InfluxDB Data Explorer provides two options for querying data with Flux:

    • Query Builder (default): Visual query builder that lets you select the time range, measurement, tags, and fields to query.
    • Script Editor: In-browser code editor for composing and running Flux scripts.

    Query builder

    To build and execute a Flux query with the query builder:

    1. In the FROM column, select the bucket to query. For this tutorial, select the get-started bucket.

    2. In the next filter column, select _measurement from the column dropdown menu, and then select the home measurement.

    3. (Optional) To query a specific field or fields, in the next filter column, select _field from the column dropdown menu, and then select the fields to query. In this tutorial, there are three fields: co, hum, and temp.

    4. (Optional) To query by specific tag values, in the next filter column, select then tag column from the column dropdown menu, and then select the tag values to filter by. In this tutorial, the tag only tag column is room.

    5. (Optional) In the Aggregate Function pane, select an aggregate or selector function to use to downsample the data. The default aggregate function is mean.

    6. In the time range dropdown menu, select Custom Time Range, and select the following dates from the date selectors:

      • Start: 2022-01-01 08:00:00

      • Stop: 2022-01-01 20:00:01

        Note the addition of one second to the stop time. In Flux, stop times are exclusive and will exclude points with that timestamp. By adding one second, the query will include all points to 2022-01-01 20:00:00.

    7. Click Submit to execute the query with the selected filters and operations and display the result.


    Script editor

    To write and execute a Flux query with the query builder:

    1. In the Data Explorer, click Script Editor.

    2. Write your Flux query in the Script Editor text field.

      Note: You can either hand-write the functions or you can use the function list to the right of the script editor to search for and inject functions.

      1. Use from() and specify the bucket to query with the bucket parameter. For this tutorial, query the get-started bucket.

      2. Use range() to specify the time range to query. The start parameter defines the earliest time to include in results. The stop parameter specifies the latest time (exclusively) to include in results.

        • start: 2022-01-01T08:00:00Z

        • stop: 2022-01-01T20:00:01Z

          Note the addition of one second to the stop time. In Flux, stop times are exclusive and will exclude points with that timestamp. By adding one second, the query will include all points to 2022-01-01 20:00:00.

        If you want to use the start and stop times selected in the time selection dropdown menu, use v.timeRangeStart and v.timeRangeStop as the values for the start and stop parameters.

      3. Use filter() to filter results by the home measurement.

      4. (Optional) Use filter() to filter results by a specific field. In this tutorial, there are three fields: co, hum, and temp.

      5. (Optional) Use filter() to filter results by specific tag values. In this tutorial, there is one tag, room, with two potential values: Living Room or Kitchen.

      1. from(bucket: "get-started")
      2. |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
      3. |> filter(fn: (r) => r._measurement == "home")
      4. |> filter(fn: (r) => r._field== "co" or r._field == "hum" or r._field == "temp")
    3. Click Submit to execute the query with the selected filters and operations and display the result.

  2. If you haven’t already, download, install, and configure the influx CLI.

  3. Use the influx query command to query InfluxDB using Flux.

    Provide the following:

  1. influx query '
  2. from(bucket: "get-started")
  3. |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
  4. |> filter(fn: (r) => r._measurement == "home")
  5. |> filter(fn: (r) => r._field== "co" or r._field == "hum" or r._field == "temp")
  6. '

To query data from InfluxDB using Flux and the InfluxDB HTTP API, send a request to the InfluxDB API /api/v2/query endpoint using the POST request method.

  1. POST http://localhost:8086/api/v2/query

Include the following with your request:

  • Headers:
    • Authorization: Token <INFLUX_TOKEN>
    • Content-Type: application/vnd.flux
    • Accept: application/csv
    • (Optional) Accept-Encoding: gzip
  • Query parameters:
    • org: InfluxDB organization name
  • Request body: Flux query as plain text

The following example uses cURL and the InfluxDB API to query data with Flux:

  1. curl --request POST \
  2. "$INFLUX_HOST/api/v2/query?org=$INFLUX_ORG&bucket=get-started" \
  3. --header "Authorization: Token $INFLUX_TOKEN" \
  4. --header "Content-Type: application/vnd.flux" \
  5. --header "Accept: application/csv" \
  6. --data 'from(bucket: "get-started")
  7. |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
  8. |> filter(fn: (r) => r._measurement == "home")
  9. |> filter(fn: (r) => r._field== "co" or r._field == "hum" or r._field == "temp")
  10. '

The InfluxDB /api/v2/query endpoint returns query results in annotated CSV.

Flux query results

View Flux query results

_start and _stop columns have been omitted. These columns, by default, represent the query time bounds and are added by range().

_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeKitchenco0
2022-01-01T09:00:00ZhomeKitchenco0
2022-01-01T10:00:00ZhomeKitchenco0
2022-01-01T11:00:00ZhomeKitchenco0
2022-01-01T12:00:00ZhomeKitchenco0
2022-01-01T13:00:00ZhomeKitchenco1
2022-01-01T14:00:00ZhomeKitchenco1
2022-01-01T15:00:00ZhomeKitchenco3
2022-01-01T16:00:00ZhomeKitchenco7
2022-01-01T17:00:00ZhomeKitchenco9
2022-01-01T18:00:00ZhomeKitchenco18
2022-01-01T19:00:00ZhomeKitchenco22
2022-01-01T20:00:00ZhomeKitchenco26
_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeKitchenhum35.9
2022-01-01T09:00:00ZhomeKitchenhum36.2
2022-01-01T10:00:00ZhomeKitchenhum36.1
2022-01-01T11:00:00ZhomeKitchenhum36
2022-01-01T12:00:00ZhomeKitchenhum36
2022-01-01T13:00:00ZhomeKitchenhum36.5
2022-01-01T14:00:00ZhomeKitchenhum36.3
2022-01-01T15:00:00ZhomeKitchenhum36.2
2022-01-01T16:00:00ZhomeKitchenhum36
2022-01-01T17:00:00ZhomeKitchenhum36
2022-01-01T18:00:00ZhomeKitchenhum36.9
2022-01-01T19:00:00ZhomeKitchenhum36.6
2022-01-01T20:00:00ZhomeKitchenhum36.5
_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeKitchentemp21
2022-01-01T09:00:00ZhomeKitchentemp23
2022-01-01T10:00:00ZhomeKitchentemp22.7
2022-01-01T11:00:00ZhomeKitchentemp22.4
2022-01-01T12:00:00ZhomeKitchentemp22.5
2022-01-01T13:00:00ZhomeKitchentemp22.8
2022-01-01T14:00:00ZhomeKitchentemp22.8
2022-01-01T15:00:00ZhomeKitchentemp22.7
2022-01-01T16:00:00ZhomeKitchentemp22.4
2022-01-01T17:00:00ZhomeKitchentemp22.7
2022-01-01T18:00:00ZhomeKitchentemp23.3
2022-01-01T19:00:00ZhomeKitchentemp23.1
2022-01-01T20:00:00ZhomeKitchentemp22.7
_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeLiving Roomco0
2022-01-01T09:00:00ZhomeLiving Roomco0
2022-01-01T10:00:00ZhomeLiving Roomco0
2022-01-01T11:00:00ZhomeLiving Roomco0
2022-01-01T12:00:00ZhomeLiving Roomco0
2022-01-01T13:00:00ZhomeLiving Roomco0
2022-01-01T14:00:00ZhomeLiving Roomco0
2022-01-01T15:00:00ZhomeLiving Roomco1
2022-01-01T16:00:00ZhomeLiving Roomco4
2022-01-01T17:00:00ZhomeLiving Roomco5
2022-01-01T18:00:00ZhomeLiving Roomco9
2022-01-01T19:00:00ZhomeLiving Roomco14
2022-01-01T20:00:00ZhomeLiving Roomco17
_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeLiving Roomhum35.9
2022-01-01T09:00:00ZhomeLiving Roomhum35.9
2022-01-01T10:00:00ZhomeLiving Roomhum36
2022-01-01T11:00:00ZhomeLiving Roomhum36
2022-01-01T12:00:00ZhomeLiving Roomhum35.9
2022-01-01T13:00:00ZhomeLiving Roomhum36
2022-01-01T14:00:00ZhomeLiving Roomhum36.1
2022-01-01T15:00:00ZhomeLiving Roomhum36.1
2022-01-01T16:00:00ZhomeLiving Roomhum36
2022-01-01T17:00:00ZhomeLiving Roomhum35.9
2022-01-01T18:00:00ZhomeLiving Roomhum36.2
2022-01-01T19:00:00ZhomeLiving Roomhum36.3
2022-01-01T20:00:00ZhomeLiving Roomhum36.4
_time_measurementroom_field_value
2022-01-01T08:00:00ZhomeLiving Roomtemp21.1
2022-01-01T09:00:00ZhomeLiving Roomtemp21.4
2022-01-01T10:00:00ZhomeLiving Roomtemp21.8
2022-01-01T11:00:00ZhomeLiving Roomtemp22.2
2022-01-01T12:00:00ZhomeLiving Roomtemp22.2
2022-01-01T13:00:00ZhomeLiving Roomtemp22.4
2022-01-01T14:00:00ZhomeLiving Roomtemp22.3
2022-01-01T15:00:00ZhomeLiving Roomtemp22.3
2022-01-01T16:00:00ZhomeLiving Roomtemp22.4
2022-01-01T17:00:00ZhomeLiving Roomtemp22.6
2022-01-01T18:00:00ZhomeLiving Roomtemp22.8
2022-01-01T19:00:00ZhomeLiving Roomtemp22.5
2022-01-01T20:00:00ZhomeLiving Roomtemp22.2

Query data with InfluxQL

InfluxQL is a SQL-like query language similar to most SQL languages, but specifically designed to query time series data from InfluxDB 0.x and 1.x.

Map databases and retention policies to buckets

Because InfluxQL was developed for earlier versions of InfluxDB, it depends on databases and retention policies (DBRP) which have been replaced by buckets in InfluxDB 2.7. To use InfluxQL with InfluxDB 2.7, first map database and retention policy (DBRP) combinations to an InfluxDB bucket.

InfluxQL query basics

When querying InfluxDB with InfluxQL, the most basic query includes the following statements and clauses:

  • SELECT: Specify which fields and tags to query.
  • FROM: Specify the measurement to query. Use the measurement name or a fully-qualified measurement name which includes the database and retention policy. For example: db.rp.measurement.
  • WHERE: (Optional) Filter data based on fields, tags, and time.

The following InfluxQL query returns the co, hum, and temp fields and the room tag stored in the home measurement with timestamps between 2022-01-01T08:00:00Z and 2022-01-01T20:00:00Z.

  1. SELECT co,hum,temp,room FROM "get-started".autogen.home WHERE time >= '2022-01-01T08:00:00Z' AND time <= '2022-01-01T20:00:00Z'

These are just the fundamentals of the InfluxQL syntax. For more in-depth information, see the InfluxQL documentation.

Execute an InfluxQL query

Use the influx CLI, or InfluxDB API to execute InfluxQL queries.

InfluxDB UI influx CLI InfluxDB API

The InflxuDB UI does not support InfluxQL

The InfluxDB 2.7 UI does not provide a way to query data with InfluxQL. For a user interface that builds and executes InfluxQL queries, consider using Chronograf or Grafana with InfluxDB 2.7.

Authentication credentials

The examples below assume your InfluxDB host, organization, and token are provided by either the active influx CLI configuration or by environment variables (INFLUX_HOST, INFLUX_ORG, and INFLUX_TOKEN). If you do not have a CLI configuration set up or the environment variables set, include these required credentials for each command with the following flags:

  • --host: InfluxDB host
  • -o, --org or --org-id: InfluxDB organization name or ID
  • -t, --token: InfluxDB API token
  1. If you haven’t already, download, install, and configure the influx CLI.

  2. Use the influx v1 shell command to start an InfluxQL shell and query InfluxDB using InfluxQL. Provide the following:

    1. influx v1 shell
  3. Enter an InfluxQL query and press Enter ↵.

    1. SELECT co,hum,temp,room FROM "get-started".autogen.home WHERE time >= '2022-01-01T08:00:00Z' AND time <= '2022-01-01T20:00:00Z'

To query data from InfluxDB using InfluxQL and the InfluxDB HTTP API, send a request to the InfluxDB API /query 1.X compatibility endpoint using the POST request method.

  1. POST http://localhost:8086/query

Include the following with your request:

  • Headers:

    • Authorization: Token <INFLUX_TOKEN>
    • Accept: application/json
    • (Optional) Accept-Encoding: gzip
  • Query parameters:

    • db: Database to query.

    • rp: Retention policy to query data from.

    • q: InfluxQL query to execute.

    • epoch: (Optional) Return results with Unix timestamps of a specified precision instead of RFC3339 timestamps. The following precisions are available:

      • ns - nanoseconds
      • u or µ - microseconds
      • ms - milliseconds
      • s - seconds
      • m - minutes
      • h - hours
  • Request body: Flux query as plain text

The following example uses cURL and the InfluxDB API to query data with InfluxQL:

  1. curl --get "$INFLUX_HOST/query?org=$INFLUX_ORG&bucket=get-started" \
  2. --header "Authorization: Token $INFLUX_TOKEN" \
  3. --data-urlencode "db=get-started" \
  4. --data-urlencode "rp=autogen" \
  5. --data-urlencode "q=SELECT co,hum,temp,room FROM home WHERE time >= '2022-01-01T08:00:00Z' AND time <= '2022-01-01T20:00:00Z'"

The InfluxDB /write 1.x compatibility endpoint returns query results in JSON format.

InfluxQL query results

View InfluxQL query results

timeroomcohumtemp
2022-01-01T08:00:00ZKitchen035.921
2022-01-01T08:00:00ZLiving Room035.921.1
2022-01-01T09:00:00ZKitchen036.223
2022-01-01T09:00:00ZLiving Room035.921.4
2022-01-01T10:00:00ZKitchen036.122.7
2022-01-01T10:00:00ZLiving Room03621.8
2022-01-01T11:00:00ZKitchen03622.4
2022-01-01T11:00:00ZLiving Room03622.2
2022-01-01T12:00:00ZKitchen03622.5
2022-01-01T12:00:00ZLiving Room035.922.2
2022-01-01T13:00:00ZKitchen136.522.8
2022-01-01T13:00:00ZLiving Room03622.4
2022-01-01T14:00:00ZKitchen136.322.8
2022-01-01T14:00:00ZLiving Room036.122.3
2022-01-01T15:00:00ZKitchen336.222.7
2022-01-01T15:00:00ZLiving Room136.122.3
2022-01-01T16:00:00ZKitchen73622.4
2022-01-01T16:00:00ZLiving Room43622.4
2022-01-01T17:00:00ZKitchen93622.7
2022-01-01T17:00:00ZLiving Room535.922.6
2022-01-01T18:00:00ZKitchen1836.923.3
2022-01-01T18:00:00ZLiving Room936.222.8
2022-01-01T19:00:00ZKitchen2236.623.1
2022-01-01T19:00:00ZLiving Room1436.322.5
2022-01-01T20:00:00ZKitchen2636.522.7
2022-01-01T20:00:00ZLiving Room1736.422.2

Congratulations! You’ve learned the basics of querying data in InfluxDB. For a deep dive into all the ways you can query InfluxDB, see the Query data in InfluxDB section of documentation.

Let’s move on to more advanced data processing queries and automating queries with InfluxDB tasks.

Write data Process data