Documentation

Get started querying data

InfluxDB Clustered supports multiple query languages:

  • SQL: Traditional SQL powered by the Apache Arrow DataFusion query engine. The supported SQL syntax is similar to PostgreSQL.
  • InfluxQL: An SQL-like query language designed to query time series data stored in InfluxDB.

This tutorial walks you through the fundamentals of querying data in InfluxDB and focuses on using SQL to query your time series data. The InfluxDB SQL implementation is built using Arrow Flight SQL, a protocol for interacting with SQL databases using the Arrow in-memory format and the Flight RPC framework. It leverages the performance of Apache Arrow with the simplicity of SQL.

The examples in this section of the tutorial query the get-started database for data written in the Get started writing data section.

Tools to execute queries

InfluxDB Clustered supports many different tools for querying data, including:

* Covered in this tutorial

/api/v2/query not supported

The /api/v2/query API endpoint and associated tooling, such as the influx CLI and InfluxDB v2 client libraries, aren’t supported in InfluxDB Clustered.

SQL query basics

The InfluxDB Clustered SQL implementation is powered by the Apache Arrow DataFusion query engine which provides an SQL syntax similar to PostgreSQL.

This is a brief introduction to writing SQL queries for InfluxDB. For more in-depth details, see Query data with SQL.

InfluxDB SQL queries most commonly include the following clauses:

* Required
  • * SELECT: Identify specific fields and tags to query from a measurement or use the wildcard alias (*) to select all fields and tags from a measurement.
  • * FROM: Identify the measurement to query. If coming from an 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 within time bounds from each room
SELECT
  avg(temp),
  avg(hum),
  room
FROM
  home
WHERE
  time >= '2025-06-06T08:00:00Z'
  AND time <= '2025-06-06T20:00:00Z'
GROUP BY
  room
  • Copy
  • Fill window

Example SQL queries

Select all data in a measurement
SELECT * FROM home
  • Copy
  • Fill window
Select all data in a measurement within time bounds
SELECT
  *
FROM
  home
WHERE
  time >= '2025-06-06T08:00:00Z'
  AND time <= '2025-06-06T20:00:00Z'
  • Copy
  • Fill window
Select a specific field within relative time bounds
SELECT temp FROM home WHERE time >= now() - INTERVAL '1 day'
  • Copy
  • Fill window
Select specific fields and tags from a measurement
SELECT temp, room FROM home
  • Copy
  • Fill window
Select data based on tag value
SELECT * FROM home WHERE room = 'Kitchen'
  • Copy
  • Fill window
Select data based on tag value within time bounds
SELECT
  *
FROM
  home
WHERE
  time >= '2025-06-06T08:00:00Z'
  AND time <= '2025-06-06T20:00:00Z'
  AND room = 'Living Room'
  • Copy
  • Fill window
Downsample data by applying interval-based aggregates
SELECT
  DATE_BIN(INTERVAL '1 hour', time, '2025-06-06T00:00:00Z'::TIMESTAMP) as _time,
  room,
  selector_max(temp, time)['value'] AS 'max temp'
FROM
  home
GROUP BY
  _time,
  'max temp',
  room
ORDER BY room, _time
  • Copy
  • Fill window

Execute an SQL query

Get started with one of the following tools for querying data stored in an InfluxDB Clustered database:

  • influxctl CLI: Query data from your command-line using the influxctl CLI.
  • influx3 CLI: Query data from your terminal command-line using the Python-based influx3 CLI.
  • InfluxDB 3 client libraries: Use language-specific (Python, Go, etc.) clients to execute queries in your terminal or custom code.
  • Grafana: Use the FlightSQL Data Source plugin, to query, connect, and visualize data.

For this example, use the following query to select all the data written to the get-started database between 2025-06-06T08:00:00Z and 2025-06-06T20:00:00Z.

SELECT
  *
FROM
  home
WHERE
  time >= '2025-06-06T08:00:00Z'
  AND time <= '2025-06-06T20:00:00Z'
  • Copy
  • Fill window

Some examples in this getting started tutorial assume your InfluxDB credentials (URL and token) are provided by environment variables.

Use the influxdb_client_3 client library module to integrate InfluxDB Clustered with your Python code. The client library supports writing data to InfluxDB and querying data using SQL or InfluxQL.

The following steps include setting up a Python virtual environment already covered in Get started writing data. If your project’s virtual environment is already running, skip to step 3.

  1. Open a terminal in the influxdb_py_client module directory you created in the Write data section:

    1. To create and activate your Python virtual environment, enter the following command in your terminal:

      python -m venv envs/virtual-env && . ./envs/virtual-env/bin/activate
      
      • Copy
      • Fill window
    2. Install the following dependencies:

      * Already installed in the Write data section

      • influxdb3-python*: Provides the InfluxDB influxdb_client_3 Python client library module and also installs the pyarrow package for working with Arrow data returned from queries.
      • pandas: Provides pandas functions, modules, and data structures for analyzing and manipulating data.
      • tabulate: Provides the tabulate function for formatting tabular data. pandas requires this module for formatting data as Markdown.

      In your terminal, enter the following command:

      pip install influxdb3-python pandas tabulate
      
      • Copy
      • Fill window
    3. In your terminal or editor, create a new file for your code–for example: query.py.

  2. In query.py, enter the following sample code:

    from influxdb_client_3 import InfluxDBClient3
    
    client = InfluxDBClient3(
        host=f"cluster-host.com",
        token=f"DATABASE_TOKEN",
        database=f"get-started",
    )
    
    sql = '''
      SELECT
        *
      FROM
        home
      WHERE
        time >= '2025-06-06T08:00:00Z'
        AND time <= '2025-06-06T20:00:00Z'
    '''
    
    table = client.query(query=sql)
    assert table['room'], "Expect table to have room column."
    print(table.to_pandas().to_markdown())
    
    • Copy
    • Fill window

Important: If using Windows, specify the Windows certificate path

The sample code does the following:

  1. Imports the InfluxDBClient3 constructor from the influxdb_client_3 module.

  2. Calls the InfluxDBClient3() constructor method with credentials to instantiate an InfluxDB client with the following credentials:

    • host: InfluxDB cluster URL (without https:// protocol or trailing slash)
    • token: a database token with read access to the specified database. Store this in a secret store or environment variable to avoid exposing the raw token string.
    • database: the name of the InfluxDB Clustered database to query
  3. Defines the SQL query to execute and assigns it to a query variable.

  4. Calls the client.query() method with the SQL query. query() sends a Flight request to InfluxDB, queries the database, retrieves result data from the endpoint, and then returns a pyarrow.Table assigned to the table variable.

  5. Calls the to_pandas() method to convert the Arrow table to a pandas.DataFrame.

  6. Calls the pandas.DataFrame.to_markdown() method to convert the DataFrame to a markdown table.

  7. Calls the print() method to print the markdown table to stdout.

  8. Enter the following command to run the program and query your InfluxDB cluster:

    python query.py
    
    • Copy
    • Fill window

View returned markdown table

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 Clustered, see the Query data in InfluxDB section of documentation.


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

Now Generally Available

InfluxDB 3 Core and Enterprise

Start fast. Scale faster.

Get the Updates

InfluxDB 3 Core is an open source, high-speed, recent-data engine that collects and processes data in real-time and persists it to local disk or object storage. InfluxDB 3 Enterprise builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries and optimized storage. A free tier of InfluxDB 3 Enterprise is available for non-commercial at-home or hobbyist use.

For more information, check out: