Inner Queries
Subquery
Subqueries are available starting from QQL 5.7.3.
An Inner Query (or Subquery, or Inner Select) is a query inside another query.
A subquery is executed before the main query. Its result is fully materialized in memory and represented as an array of objects.
This means:
-
The subquery runs independently and has its own context.
-
Its results are cached and reused in the outer query.
-
It behaves like a temporary in-memory dataset that can be indexed, filtered, or aggregated.
Memory usage is limited: subqueries must fit into the allocated buffer. Very large subqueries may fail.
Limitations
-
Non-Correlated: Subqueries cannot reference fields from the outer stream or another subquery.
-
Subqueries are not streaming: They are executed once and stored in memory.
-
Memory: The result must fit within the memory limit.
-
Performance: Be aware that using multiple subqueries may impact performance.
-
Complex nesting is supported, but each layer multiplies memory consumption.
Examples
-- Select entries from BITFINEX only for symbols from securities
SELECT * FROM BITFINEX
WHERE symbol IN (SELECT symbol FROM securities).symbol
-- Select events only for instruments with traded volume > 0.1
SELECT sum{}(t.size) AS volume
FROM bitfinex
ARRAY JOIN (entries AS array(TradeEntry)) AS t
GROUP BY symbol
HAVING volume > (
SELECT avg{}(t.size) AS v
FROM bitfinex
ARRAY JOIN (entries AS array(TradeEntry)) AS t
).v[0]
-- Select traded volume for symbols with volume > avg volume
select sum{}(t.size) as volume from bitfinex
array join (entries as array(TradeEntry)) as t
group by symbol
having volume > (
select avg{}(t.size) as v from bitfinex
array join (entries as array(TradeEntry)) as t
).v[0]
-- Arithmetic operations with subquery results
with
(select byteField from alltypesrand where symbol == 'S1' limit 5).byteField as a,
(select byteField from alltypesrand where symbol == 'S2' limit 5).byteField as b
select a, b, a * b
from alltypesrand
limit 1
Inline View
Inline views are available starting from QQL 5.6.78.
With inline views, you can embed a select statement within the from clause of another select statement. Queries can be combined in a sequential manner, facilitating layered data analysis.
Inline views allow for breaking down complex queries into smaller components where each subquery performs a distinct task.
Limitations
- Scope: Inline view can only be used in the
fromclause. - Performance: Be aware that using multiple inline views may impact performance.
Use Cases
1. Data resampling before final calculations
This query gets the latest value in each millisecond and calculates the sum for each second.
select sum{}(v) from (
select value as v from stream
over time (1ms)
)
over time (1s)
2. Alternative to having clause
In this case, we use the inner query because the min > 27020 condition can affect the result set.
SELECT * FROM (
SELECT min{}(entries[this is TradeEntry][0].price) as 'min'
FROM "COINBASE"
OVER TIME(1s)
WHERE symbol == 'BTC/USDT'
) WHERE min > 27020
3. More intuitive and performant ways of making queries
This query counts the distinct number of symbols.
Without an inline views, the COLLECT_UNIQUE function accumulates symbols in an array that can require large memory allocations:
SELECT SIZE(COLLECT_UNIQUE{}(symbol)) FROM "securities"
With an inline views:
SELECT COUNT{}() FROM (SELECT DISTINCT symbol FROM "securities")