Louis Davidson is one of a kind:
If there is one SQL keyword that causes more fear than any other, it’s
DISTINCT. When I see it in a query, I immediately start to worry about just how much work I am in for to ensure the correctness of that query. I start scanning for comments to describe why it is there, and if none are found, I know the query is probably going to be wrong.I have seen
DISTINCTused to hide bad joins, missing grouping, and even missingWHEREclauses. I have seen developers use it as a “fix-all” for data problems.In this blog, I will look at the proper use and distinctly dangerous uses of
DISTINCTand also show how you might test your query that usesDISTINCTto see what it is actually covering up.
Louis also includes one of my “favorite” coding errors: the accidental self-join. Done that one too many times to be proud of.