4 ms·
It's easier not to mess up table based filters using explicit semi-join operators (eg. in, not in, exists) instead of using regular joins because joins can intr
by snidane 4y ago
It's easier not to mess up table based filters using explicit semi-join operators (eg. in, not in, exists) instead of using regular joins because joins can introduce duplicates.
Give me 'any join' operation - ie. just select the first value instead of all, and I'll happily use joins more. They are actually more intuitive.
It's not that relational algebra is untintuitive. It's because standard SQL sucks.
- magicalhippo 4y agoIndeed, I've taught myself to only use JOIN when I actually need some data from the table I join. For everything else I use EXISTS and friends. I was thinking SQL could do with a keyword for that, like maybe FILTER, that looks like a JOIN but works like EXISTS.
- snidane 4y agoClickhouse implements an explicit SEMI join. It can be called semi or any, it doesn't really matter. It's just another join modifier [OUTER|SEMI|ANTI|ANY|ASOF] https://clickhouse.com/docs/en/sql-reference/statements/select/join/ https://clickhouse.com/docs/en/sql-reference/statements/sele...
- fiddlerwoaroof 4y ago“First” doesn’t make sense without an order
- snidane 4y agoIt does make sense for semi-joins. I care about the key, not the value. Random order is also a valid order.
- deleted 4y ago[deleted]
- jodrellblank 4y agoIt even has its own tag on StackOverflow: https://stackoverflow.com/questions/tagged/greatest-n-per-group https://stackoverflow.com/questions/tagged/greatest-n-per-gr... People who want it, want it with an order. Look at https://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column/123481 https://stackoverflow.com/questions/121387/fetch-the-row-whi... and https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group https://stackoverflow.com/questions/3800551/select-first-row... and https://stackoverflow.com/questions/8748986/get-records-with-highest-smallest-whatever-per-group/8749095 https://stackoverflow.com/questions/8748986/get-records-with... and their combined thousands of votes and dozens of answers, all full of awkward workaround or ill-performing or specialised-for-one-database-engine code for this common and desirable thing which would be trivial with a couple of boring loops in Python.
- nerdponx 4y agoMy problem with semijoins is that the semantics of "what exactly does a SELECT evaluate to inside an expression" are sometimes murky and might vary across databases.
- magicalhippo 4y agoCould you expand a little?
- nerdponx 4y agoIf I write WHERE x IN (SELECT ...) what the heck is the result of evaluating the inner query, in the outer expression? Maybe I am missing something, but the exact meaning to vary a lot across different databases. Some seem to have a standalone "table" data type, while others don't.
- magicalhippo 4y agoI might be missing something as I'm self-taught, but the inner select specifies a set, and you "just" do a simple set membership test? How it's implemented is as usual up to the database server implementation. Ones I've used creates a temporary table (like it does in so many other cases), and as such EXISTS is usually faster. But I wouldn't rely on this when moving to another implementation, and use the query planner to see, just as I'd view the assembly output when moving to a new compiler. Again, I don't have tons of experience, so concrete (counter) examples are welcome.