Documentation

Subqueries

A subquery is a query that is nested in the FROM clause of another query. Use a subquery to apply a query as a condition in the enclosing query. Subqueries offer functionality similar to nested functions and the SQL HAVING clause.

Note: InfluxQL does not support a HAVING clause.

Syntax

SELECT_clause FROM ( SELECT_statement ) [...]

InfluxDB performs the subquery first and the main query second.

The main query surrounds the subquery and requires at least the SELECT clause and the FROM clause. The main query supports all clauses listed in InfluxQL 2.x documentation.

The subquery appears in the main query’s FROM clause, and it requires surrounding parentheses. The subquery also supports all clauses listed in InfluxQL 2.x documentation.

InfluxQL supports multiple nested subqueries per main query. Sample syntax for multiple subqueries:

SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]

Note: #### Improve performance of time-bound subqueries To improve the performance of InfluxQL queries with time-bound subqueries, apply the WHERE time clause to the outer query instead of the inner query. For example, the following queries return the same results, but the query with time bounds on the outer query is more performant than the query with time bounds on the inner query:

SELECT inner_value AS value FROM (SELECT raw_value as inner_value)
WHERE time >= '2022-07-19T21:00:00Z'
AND time <= '2022-07-20T22:00:00Z'
Time bounds on the inner query
SELECT inner_value AS value FROM (
  SELECT raw_value as inner_value
  WHERE time >= '2022-07-19T21:00:00Z'
  AND time <= '2022-07-20T22:00:00Z'
)

Examples

Calculate the SUM() of several MAX() values

Calculate the MEAN() difference between two fields

Calculate several MEAN() values and place a condition on those mean values

Calculate the SUM() of several DERIVATIVE() values

Common issues with subqueries

Multiple statements in a subquery

InfluxQL supports multiple nested subqueries per main query:

SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]
                     ------------------   ----------------
                         Subquery 1          Subquery 2

InfluxQL does not support multiple SELECT statements per subquery:

SELECT_clause FROM (SELECT_statement; SELECT_statement) [...]

The system returns a parsing error if a subquery includes multiple SELECT statements.


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

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.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

InfluxDB Cloud powered by TSM