Subqueries on Elasticsearch: No Rewrite, No Warehouse Copy
SoftClient4ES 0.24.0 runs subqueries and derived tables on live Elasticsearch data, including the nested SQL your BI tool writes for you.
SoftClient4ES 0.24.0 is out. This post covers subqueries and derived tables: what they unlock for analysts and BI teams, what works with nothing extra to install, and what needs the relational engine. A companion post covers CTEs and the set operators.
”Just use Custom SQL” was never a workaround
If your team has pointed a dashboard tool at Elasticsearch, you have heard the advice. The drag-and-drop query fails? Write Custom SQL. You do the hard part, and the tool draws the chart.
Tableau’s own documentation explains the catch. To apply its filters and groupings, Tableau “must wrap the custom SQL statement within a select statement.” That wrapper, SELECT … FROM ( your SQL ), is a subquery. Superset’s virtual datasets work the same way: the chart query is built around the dataset’s SQL, as a subquery in FROM.
So on a SQL layer without subqueries, the escape hatch breaks for the same reason the original query did. What happens next is familiar, and it costs money:
- an engineer rewrites the analyst’s question as a JOIN, or pre-computes it: one ticket per new question;
- the data gets copied into a warehouse, only so that someone can run a nested query on it;
- or nobody asks the question.
What 0.24.0 changes
Subqueries and derived tables now run on your Elasticsearch indices. Here are four everyday questions, asked of two ordinary indices, customers and orders.
Filter one index by another. Which orders came from gold-tier customers?
SELECT id, customer_id, amountFROM ordersWHERE customer_id IN (SELECT id FROM customers WHERE tier = 'gold');Compare with a computed value. Which orders are above our average order?
SELECT id, amountFROM ordersWHERE amount > (SELECT AVG(amount) FROM orders);Find what is missing. Which customers have never ordered?
SELECT c.id, c.nameFROM customers cWHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);Query a query. Which countries have passed 10,000 in revenue?
SELECT d.country, d.revenueFROM (SELECT country, SUM(amount) AS revenue FROM orders GROUP BY country) AS dWHERE d.revenue > 10000;NOT IN, EXISTS and the ANY / SOME / ALL comparisons work too, and the same WHERE subqueries are accepted in DELETE and UPDATE.
That last shape, a SELECT inside FROM, is called a derived table, and it is exactly what BI tools generate. This is the shape of query Superset produces for a chart on a virtual dataset that joins the two indices:
SELECT tier, SUM(amount) AS revenueFROM (SELECT o.amount, c.tier FROM orders AS o JOIN customers AS c ON o.customer_id = c.id) AS virtual_tableGROUP BY tierORDER BY revenue DESC;Nobody rewrites it, nobody copies the data, and nobody files a ticket. The analyst asks the question the way they would in any SQL database, and Superset’s generated SQL runs as written.
What runs where: the short version
There are two families. It pays to know which one your query is in.
Runs everywhere, nothing to install. A WHERE subquery that does not refer to the outer query, like the first two examples, runs on Elasticsearch itself. The inner query runs first, and its values become an ordinary filter on the outer one. This works in the REPL, the JDBC driver, ADBC and Arrow Flight SQL, and it is part of the Apache-2.0 core.
Runs on the relational engine. Two forms need SoftClient4ES’s relational engine, softclient4es-arrow-extensions 0.3.4, the same engine that runs cross-index JOINs: a correlated subquery (one that refers to the outer row, like “never ordered”) and a derived table. You almost certainly have it already. The JDBC driver, the ADBC driver and the Arrow Flight SQL server bundle it, and the REPL installer adds it by default. A REPL installed with --no-extensions does not have it. It refuses those statements with a message naming the construct and the missing jar, and never quietly runs them against a single index.
What it costs:
- The core engine is Apache-2.0. The relational engine is under the Elastic License 2.0: free to use, not open source.
- On the free Community plan, a query may contain up to two JOINs. A correlated subquery counts as one, because underneath it is a join. A derived table costs nothing by itself; only the JOINs inside it count.
For BI teams: where your tool stands
- Superset (with its dedicated dialect), DBeaver and DataGrip are in our Tested tier.
- Tableau is in our Compatible tier. It connects over JDBC, and since 0.24.0 the derived-table wrapper it generates runs on the relational engine. But Tableau has not been through our formal regression suite, and Extract mode is untested. Treat it as a path to evaluate on your own workbooks, not as a certified one.
Built to say no out loud
An analyst can only trust a SQL layer that says so when it cannot answer. So:
- The “runs everywhere” family turns the inner query into a list of values, and that list is capped at 65,536 distinct values. Past the cap, the statement fails with an error instead of silently truncating the list. For a longer list, write the condition as a JOIN.
- Forms that are not supported yet, such as a subquery in the
SELECTlist, inHAVING, orLATERAL, are refused rather than guessed at. Most refusals name the construct and suggest a rewrite. The full list is in the docs. - Two cases are not refused, so make them habits. In a correlated subquery, always qualify the outer column (
c.id, notid): a bare name is read as the subquery’s own column, which makes it a legal but different query, and no SQL engine can tell that apart from what you meant. And when the inner query of aNOT INhas aGROUP BY, filter out its NULLs explicitly (WHERE col IS NOT NULL): Elasticsearch’s grouping drops the NULL group, so the engine cannot see it.
Try it
curl -fsSL https://raw.githubusercontent.com/SOFTNETWORK-APP/SoftClient4ES/main/install.sh | bashsoftclient4es --host localhost --port 9200The installer includes the relational engine, so every query above runs out of the box. In the REPL, a plain query without a LIMIT, like the first two examples, streams its rows: type consume to fetch them. Install with --no-extensions and you get the pure Apache-2.0 core, where the first two examples still run.
From a BI tool or a SQL client, use the JDBC driver 0.3.4. It bundles the relational engine, which needs Java 11 or later. These features ship in core 0.24.0, with the relational engine at 0.3.4.
Where to Go Next
- Who bought in both quarters? One SQL statement on Elasticsearch: the companion post on CTEs and
UNION/INTERSECT/EXCEPT. - Subqueries and derived tables, in the SQL reference: every accepted form, with examples.
- Which forms need the relational engine: the per-venue rules, in one table.
- Stop ETL’ing Elasticsearch into your warehouse just to JOIN it: the release that introduced the relational engine.
Resources
- GitHub: SoftClient4ES Repository
- SQL Reference: Full SQL Documentation
- Discussions: Ask Questions
- LinkedIn: SoftNetwork
When did your team last turn a question into a ticket because the SQL layer could not nest it? Run that query today. If something is still refused, tell us in GitHub Discussions; that is how we decide what comes next.
P.S. In August we wrote that subqueries and CTEs were planned for Quarter 4 2026. They shipped on 26 October, in 0.24.0.