4 ms·
You can abuse it, but the lineage of comprehensions (Haskell's list monad, SQL) make it clear that longer examples are certainly useful. Haskell list comprehen
by code_biologist 4y ago
You can abuse it, but the lineage of comprehensions (Haskell's list monad, SQL) make it clear that longer examples are certainly useful.
Haskell list comprehensions limited to two clauses, SQL queries limited to a single FROM and a WHERE, or a FROM and a JOIN sound pretty limited. I use Python comprehensions to do those same kinds of data querying without leaving the Python context, so seems weird to limit myself for the same reason.
Using imperative constructs certainly isn't wrong, but they tend to crawl off the right hand side of the page.
- maxbond 4y agoYeah, I personally find a triply-nested comprehension to be more readable than a triply-nested loop. Once you get comfortable with the syntax, it's nice to be able to fit it compactly on your screen, so that your eyes can roll all over it without having to scroll. I agree with the general principle that terseness can be a detriment to readability, but sometimes it's nice for everything to be in one place. I suppose this is only true in combination with verbose function names that capture the logic of the operation you're performing, with the comprehension expressing how this logic is composed. In my mind, comprehensions are sugar over map(), filter(), etc., and before I was using comprehensions I had a mess of nested calls to these functions with lots of lambdas. Comprehensions are a big step up from that as far as readability goes.
- im3w1l 4y agoTbh I often find I would like to use variables in sql. Something like 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;
- hackandthink 4y agoCommon Table Extensions are often good enough: With americans as (select ...) select from ..., americans ... https://www.draxlr.com/blogs/common-table-expressions-and-its-example-in-postgresql/ https://www.draxlr.com/blogs/common-table-expressions-and-it...
- code_biologist 4y agoThe lack of variables is a major pain point for many devs when learning SQL. CTEs address one aspect. It's maybe not 1:1 or ideal, but lateral joins are a great way to get in-query variables and are extremely powerful for controlling cardinality! https://sqlfordevs.com/for-each-loop-lateral-join https://sqlfordevs.com/for-each-loop-lateral-join
- im3w1l 4y agoOh huh, I didn't know that was a thing. Exactly what I wanted.
- bit_for_a_byte 4y agoIt wouldn’t work as a variable, you’d get race conditions unless you explicitly wrapped both query statements in a transaction. 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.
- hyencomper 4y agoFor cases like these, I use sql views - CREATE VIEW view as SELECT * .. Then DROP VIEW view.