Documentation

Get started querying data

InfluxDB Cloud backed by InfluxDB IOx supports multiple query languages:

  • SQL: Traditional SQL powered by the Apache Arrow DataFusion query engine. The supported SQL syntax is similar to PostgreSQL.
  • Flux: A functional scripting language designed to query and process data from InfluxDB and other data sources.

This tutorial walks you through the fundamentals of querying data in InfluxDB and focuses on using SQL to query your time series data.

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

Tools to execute queries

InfluxDB supports many different tools for querying data, including:

* Covered in this tutorial

SQL query basics

InfluxDB Cloud’s SQL implementation is powered by the Apache Arrow DataFusion query engine which provides a SQL syntax similar to PostgreSQL.

This is a brief introduction to writing SQL queries for InfluxDB. For more in-depth details, see the SQL reference documentation.

InfluxDB SQL queries most commonly include the following clauses:

* Required
  • * SELECT: Identify specific fields and tags to query from a measurement or use the wild card alias (*) to select all fields and tags from a measurement.
  • * FROM: Identify the measurement to query. If coming from a SQL background, an InfluxDB measurement is the equivalent of a relational table.
  • WHERE: Only return data that meets defined conditions such as falling within a time range, containing specific tag values, etc.
  • GROUP BY: Group data into SQL partitions and apply an aggregate or selector function to each group.
-- Return the average temperature and humidity from each room
SELECT
  mean(temp),
  mean(hum),
  room
FROM
  home
WHERE
  time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
GROUP BY
  room
Example SQL queries

Select all data in a measurement

Select all data in a measurement within time bounds

Select a specific field within relative time bounds

Select specific fields and tags from a measurement

Select data based on tag value

Select data based on tag value within time bounds

Downsample data by applying interval-based aggregates

Execute a SQL query

Use the InfluxDB UI, influx CLI, or InfluxDB API to execute SQL queries. For this example, use the following query to select all the data written to the get-started bucket between 2022-01-01T08:00:00Z and 2022-01-01T20:00:00Z.

SELECT
  *
FROM
  home
WHERE
  time >= '2022-01-01T08:00:00Z'
  AND time <= '2022-01-01T20:00:00Z'
  1. Go to cloud2.influxdata.com in a browser to log in and access the InfluxDB UI.

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

  3. In the schema browser on the left, select the get-started bucket from the bucket drop-down menu. The displayed measurements and fields are read-only and are meant to show you the schema of data stored in the selected bucket.

  4. Enter the SQL query in the text editor.

  5. Click Run.

Results are displayed under the text editor.

The influx query command uses the InfluxDB /api/v2/query endpoint to query InfluxDB. This endpoint only accepts Flux queries. To use SQL with the influx CLI, wrap your SQL query in Flux and use iox.sql() to query the InfluxDB IOx storage engine with SQL. Provide the following:

  • Bucket name with the bucket parameter
  • SQL query with the query parameter

View iox.sql() Flux example

  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 "
import \"experimental/iox\"

iox.sql(
    bucket: \"get-started\",
    query: \"
        SELECT
          *
        FROM
          home
        WHERE
          time >= '2022-01-01T08:00:00Z'
          AND time <= '2022-01-01T20:00:00Z'
    \",
)"

To query data from InfluxDB using SQL 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

The /api/v2/query endpoint only accepts Flux queries. To query data with SQL, wrap your SQL query in Flux and use iox.sql() to query the InfluxDB IOx storage engine with SQL. Provide the following:

  • Bucket name with the bucket parameter
  • SQL query with the query parameter

View iox.sql() Flux example

Include the following with your request:

  • Headers:
    • Authorization: Token <INFLUX_TOKEN>
    • Content-Type: application/vnd.flux
    • Accept: application/csv
    • (Optional) Accept-Encoding: gzip
  • Request body: Flux query as plain text. In the Flux query, use iox.sql() and provide your bucket name and your SQL query.

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

curl --request POST \
"$INFLUX_HOST/api/v2/query" \
  --header "Authorization: Token $INFLUX_TOKEN" \
  --header "Content-Type: application/vnd.flux" \
  --header "Accept: application/csv" \
  --data "
    import \"experimental/iox\"

    iox.sql(
        bucket: \"get-started\",
        query: \"
            SELECT
              *
            FROM
              home
            WHERE
              time >= '2022-01-01T08:00:00Z'
              AND time <= '2022-01-01T20:00:00Z'
        \",
    )"

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

Query results

View query results

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


Was this page helpful?

Thank you for your feedback!


Linux Package Signing Key Rotation

All signed InfluxData Linux packages have been resigned with an updated key. If using Linux, you may need to update your package configuration to continue to download and verify InfluxData software packages.

For more information, see the Linux Package Signing Key Rotation blog post.

InfluxDB Cloud backed by InfluxDB IOx

All InfluxDB Cloud organizations created on or after January 31, 2023 are backed by the new InfluxDB IOx storage engine. Check the right column of your InfluxDB Cloud organization homepage to see which InfluxDB storage engine you’re using.

If powered by IOx, this is the correct documentation.

If powered by TSM, see the TSM-based InfluxDB Cloud documentation.

InfluxDB Cloud backed by InfluxDB TSM

All InfluxDB Cloud organizations created on or after January 31, 2023 are backed by the new InfluxDB IOx storage engine which enables nearly unlimited series cardinality and SQL query support. Check the right column of your InfluxDB Cloud organization homepage to see which InfluxDB storage engine you’re using.

If powered by TSM, this is the correct documentation.

If powered by IOx, see the IOx-based InfluxDB Cloud documentation.

State of the InfluxDB Cloud (IOx) documentation

The new documentation for InfluxDB Cloud backed by InfluxDB IOx is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.