SQL Tips and Tricks
github.com
SQL Tips and Tricks
1–10 of 168 posts
Re: SQL Tips and Tricks
#2[flagged]
Re: SQL Tips and Tricks
#3[flagged]
[deleted]
Re: SQL Tips and Tricks
#4I am bad at SQL so this is great
Re: SQL Tips and Tricks
#5Wow, that EXCEPT trick is neat! ~10 years of using SQL almost daily, and I never knew...
Re: SQL Tips and Tricks
#6Leading comma is nice in SELECT statements because you can comment toggle individual lines. Indenting your code to make it more readable is basically what anyone with room temperature IQ does automatically. A lot of these other tips look like they're designed to deal with SQL design flaws, like how handling nulls isn't well defined in the spec so it's up to each implementation to do whatever it wants.
Re: SQL Tips and Tricks
#7Not shown: stop using SELECT *. You almost certainly do not need the entire width of the table, and by doing so, you add more data to filter and transmit, and also prevent semijoins, which are awesome.
Re: SQL Tips and Tricks
#8I remember doing the "WHERE 1=1" trick in my last job and it causing a... let's say "unproductive", discussion in the pull-request.
Re: SQL Tips and Tricks
#9One more point in the "Anti Join". Use EXISTS instead of IN and LEFT JOIN if you only want to check existence of a row in another large table / subquery based on the conditions. EXISTS returns true as soon as it has found a hit. In case of LEFT JOIN and IN engine collects all results before evaluating.
Re: SQL Tips and Tricks
#10Never knew about QUALIFY. That's great