Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Queries that implicit SQL joins can't do?

Tags:

sql

join

I've never learned how joins work but just using select and the where clause has been sufficient for all the queries I've done. Are there cases where I can't get the right results using the WHERE clause and I have to use a JOIN? If so, could someone please provide examples? Thanks.

like image 784
Luke Avatar asked Dec 14 '22 00:12

Luke


1 Answers

Implicit joins are more than 20 years out-of-date. Why would you even consider writing code with them?

Yes, they can create problems that explicit joins don't have. Speaking about SQL Server, the left and right join implicit syntaxes are not guaranteed to return the correct results. Sometimes, they return a cross join instead of an outer join. This is a bad thing. This was true even back to SQL Server 2000 at least, and they are being phased out, so using them is an all around poor practice.

The other problem with implicit joins is that it is easy to accidentally do a cross join by forgetting one of the where conditions, especially when you are joining too many tables. By using explicit joins, you will get a syntax error if you forget to put in a join condition and a cross join must be explicitly specified as such. Again, this results in queries that return incorrect values or are fixed by using distinct to get rid of the cross join which is inefficient at best.

Moreover, if you have a cross join, the maintenance developer who comes along in a year to make a change doesn't know if it was intended or not when you use implicit joins.

I believe some ORMs also now require explicit joins.

Further, if you are using implied joins because you don't understand how joins operate, chances are high that you are writing code that, in fact, does not return the correct result because you don't know how to evaluate what the correct result would be since you don't understand what a join is meant to do.

If you write SQL code of any flavor, there is no excuse for not thoroughly understanding joins.

like image 142
HLGEM Avatar answered Dec 15 '22 13:12

HLGEM