americans := SELECT username FROM users WHERE country = "us";
SELECT url, visits FROM posts, americans ON posts.author = americans.username ORDER BY visits DESC LIMIT 10; americans := SELECT username FROM users WHERE country = "us";
SELECT url, visits FROM posts, americans ON posts.author = americans.username ORDER BY visits DESC LIMIT 10;With americans as (select ...) select from ..., americans ...
https://www.draxlr.com/blogs/common-table-expressions-and-it...
It could work as an expression which gets injected into the 2nd query. So the second query would effectively have a nested SELECT statement as defined by the americans expression.
But this has the downside that if you reuse americans you have to know that it will be recomputed at each query. So unless you know the semantics of these variable-expression things over time you get race conditions. The solution again would be to wrap everything in a transaction, but at that point you just have to constantly think about transactions, which you don’t if your query is a one-liner.
Also good luck writing a query planner that has to efficiently take into account variables.