Documentation

Get started querying data

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

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.

from(bucket: "get-started")
    |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
    |> filter(fn: (r) => r._measurement == "home")
    |> 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.

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

  2. In the left navigation bar, click 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.

      from(bucket: "get-started")
          |> range(start: 2022-01-01T08:00:00Z, stop: 2022-01-01T20:00:01Z)
          |> filter(fn: (r) => r._measurement == "home")
          |> 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.

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

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

    Provide the following:

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

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.

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:

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

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

Flux query results

View Flux query results

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.

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.

The influxdb 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:

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

    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.

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:

curl --get "$INFLUX_HOST/query?org=$INFLUX_ORG&bucket=get-started" \
  --header "Authorization: Token $INFLUX_TOKEN" \
  --data-urlencode "db=get-started" \
  --data-urlencode "rp=autogen" \
  --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

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.


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 v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered