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
Note: InfluxQL does not support a
SELECT_clause FROM ( SELECT_statement ) [...]
InfluxDB performs the subquery first and the main query second.
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,
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:
Time bounds on the outer query (recommended)
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' )
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
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
InfluxDB Cloud and InfluxDB Enterprise customers can contact InfluxData Support.