Documentation

Get started querying data

InfluxDB Cloud Dedicated 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 Cloud Dedicated 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 Cloud Dedicated.

SQL query basics

The InfluxDB Cloud Dedicated 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-08T08:00:00Z'
  AND time <= '2025-06-08T20: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-08T08:00:00Z'
  AND time <= '2025-06-08T20: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-08T08:00:00Z'
  AND time <= '2025-06-08T20: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-08T00:00:00Z') 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 Cloud Dedicated 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-08T08:00:00Z and 2025-06-08T20:00:00Z.

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

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

This tutorial assumes using Maven version 3.9, Java version >= 15, and an influxdb_java_client Maven project created in the Write data section.

  1. In your terminal or editor, change to the influxdb_java_client directory you created in the Write data section.

  2. Inside of the src/main/java/com/influxdbv3 directory, create a new file named Query.java.

  3. In Query.java, enter the following sample code:

    // Query.java
    package com.influxdbv3;
    
    import com.influxdb.v3.client.InfluxDBClient;
    import java.util.stream.Stream;
    
    /**
      * Queries an InfluxDB database using the Java client
      * library.
      **/
    public final class Query {
    
        private Query() {
            //not called
        }
    
        /**
        * @throws Exception
        */
        public static void querySQL() throws Exception {
            /**
            * Query using SQL.
            */
    
            /** Set InfluxDB credentials. **/
            final String host = "https://cluster-id.a.influxdb.io";
            final String database = "get-started";
    
            /** INFLUX_TOKEN is an environment variable you assigned to your
              * database READ token value.
              **/
            final char[] token = (System.getenv("INFLUX_TOKEN")).
            toCharArray();
    
            try (InfluxDBClient client = InfluxDBClient.getInstance(host,
            token, database)) {
                String sql =
                    """
                    SELECT time, room, temp, hum, co
                    FROM home
                    WHERE time >= '2025-06-08T08:00:00Z'
                    AND time <= '2025-06-08T20:00:00Z'""";
    
                String layoutHead = "| %-16s | %-12s | %-6s | %-6s | %-6s |%n";
                System.out.printf(
                "--------------------------------------------------------%n");
                System.out.printf(layoutHead,
                "time", "room", "co", "hum", "temp");
                System.out.printf(
                "--------------------------------------------------------%n");
                String layout = "| %-16s | %-12s | %-6s | %.1f | %.1f |%n";
    
                try (Stream<Object[]> stream = client.query(sql)) {
                    stream.forEach(row -> 
                      System.out.printf(layout,
                      row[0], row[1], row[4], row[3], row[2])
                    );
                }
            }
        }
    }
    
    • Copy
    • Fill window

    The sample code does the following:

    1. Assigns the com.influxdbv3 package name (the Maven groupId).

    2. Imports the following classes:

      • com.influxdb.v3.client.InfluxDBClient
      • java.util.stream.Stream
    3. Defines a Query class with a querySQL() method that does the following:

      1. Calls InfluxDBClient.getInstance() to instantiate a client configured with InfluxDB credentials.

        • host: your InfluxDB Cloud Dedicated cluster URL
        • database: the name of the InfluxDB Cloud Dedicated database to write to
        • token: a database token with read permission on the specified database. Store this in a secret store or environment variable to avoid exposing the raw token string.
      2. Defines a string variable (sql) for the SQL query.

      3. Defines a Markdown table format layout for headings and data rows.

      4. Calls the InfluxDBClient.query() method to send the query request with the SQL string. query() returns a stream of rows.

      5. Iterates over rows and prints the data in the specified layout to stdout.

  4. In your editor, open the src/main/java/com/influxdbv3/App.java file and replace its contents with the following sample code:

    // App.java
    
    package com.influxdbv3;
    
    /**
    * Execute the client functions.
    *
    */
    public class App {
    
        /**
        * @param args
        * @throws Exception
        */
        public static void main(final String[] args) throws Exception {
            // Write data to InfluxDB 3.
            Write.writeLineProtocol();
            // Run the SQL query.
            Query.querySQL();
        }
    }
    
    • Copy
    • Fill window
    • The App, Write, and Query classes belong to the com.influxdbv3 package (your project groupId).
    • App defines a main() function that calls Write.writeLineProtocol() and Query.querySQL().
  5. In your terminal or editor, use Maven to install dependencies and compile the project code–for example:

    mvn compile
    
    • Copy
    • Fill window
  6. Set the --add-opens=java.base/java.nio=ALL-UNNAMED Java option for your environment. The Apache Arrow Flight library requires this setting for access to the java.nio API package.

    For example, enter the following command in your terminal:

    Linux/MacOS

    export MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED"
    
    • Copy
    • Fill window

    Windows PowerShell

    $env:MAVEN_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED"
    
    • Copy
    • Fill window
  7. To run the app to write to and query InfluxDB Cloud Dedicated, execute App.main()–for example, using Maven:

    mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
    
    • Copy
    • Fill window

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 Cloud Dedicated, 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: