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.
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:
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:
Next, InfluxDB performs the main query and calculates the sum of those maximum values: 9.9640000000 + 7.2050000000 = 17.169.
Notice that the main query specifies max, not water_level, as the field key in the SUM() function.
Calculate the MEAN() difference between two fields
The query returns the average of the differences between the number of cats and dogs in the pet_daycare measurement.
InfluxDB first performs the subquery.
The subquery calculates the difference between the values in the cats field and the values in the dogs field,
and it names the output column difference:
Next, InfluxDB performs the main query and calculates the average of those differences.
Notice that the main query specifies difference as the field key in the MEAN() function.
Calculate several MEAN() values and place a condition on those mean values
The query returns all mean values of the water_level field that are greater than five.
InfluxDB first performs the subquery.
The subquery calculates MEAN() values of water_level from 2019-08-18T00:00:00Z through 2019-08-18T00:30:00Z and groups the results into 12-minute intervals. It also names the output column all_the_means:
Next, InfluxDB performs the main query and returns only those mean values that are greater than five.
Notice that the main query specifies all_the_means as the field key in the SELECT clause.
Calculate the SUM() of several DERIVATIVE() values
The query returns the sum of the derivative of average water_level values for each tag value of location.
InfluxDB first performs the subquery.
The subquery calculates the derivative of average water_level values taken at 12-minute intervals.
It performs that calculation for each tag value of location and names the output column water_level_derivative:
Next, InfluxDB performs the main query and calculates the sum of the water_level_derivative values for each tag value of location.
Notice that the main query specifies water_level_derivative, not water_level or derivative, as the field key in the SUM() function.
Common issues with subqueries
Multiple statements in a subquery
InfluxQL supports multiple nested subqueries per main query:
The system returns a parsing error if a subquery includes multiple SELECT statements.
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 v3 enhancements and InfluxDB Clustered is now generally available
New capabilities, including faster query performance and management
tooling advance the InfluxDB v3 product line.
InfluxDB Clustered is now generally available.
InfluxDB v3 performance and features
The InfluxDB v3 product line has seen significant enhancements in query
performance and has made new management tooling available. These enhancements
include an operational dashboard to monitor the health of your InfluxDB cluster,
single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management
APIs for tokens and databases.
You are currently viewing documentation specific to InfluxDB Cloud
powered by the TSM storage engine, which
offers different functionality than InfluxDB Cloud
Serverless
powered by the v3 storage engine.