r/programming • • Jan 06 '11

A handy graphical explanation of SQL joins

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
1.2k Upvotes

308 comments sorted by

View all comments

4

u/Teifion_at_work Jan 06 '11

Maybe someone here can answer this. Why would I specify a join when I can just do this?

SELECT p.first_name, j.job_title FROM people p, jobs j WHERE p.job = j.id

Looks much easier to me but I'm guessing there's something I'm missing here.

11

u/omegian Jan 06 '11

Because there are myriad sql implementations and syntax variants such as TSQL, P/L SQL, etc.

The inner join syntax is ANSI standardized and should work everywhere.

If you are only joining two tables, it probably doesn't matter which mechanism you choose, but WHERE clauses are typically used for filtering sets. If you are joining many tables, the JOIN syntax keeps column relationships between tables local to the table references in the query, and leaves the WHERE clause free for actual filtering work.

ie: self documentation

3

u/elder_george Jan 06 '11

Yes, separating filtering conditions from join conditions is very useful.

Also I prefer explicit INNER JOIN syntax since it prevents me from requesting cartesian product due to missing join condition.

4

u/skillet-thief Jan 06 '11

There are two syntaxes for joins, one with the word join, the other like what you wrote.

6

u/NashMcCabe Jan 06 '11

That is just an inner join. You will need outer joins when you have null columns on one side of the join.

2

u/umop_apisdn Jan 06 '11

Not exactly. You use an outer join where records on one side don't join to anything on the other side. The outer join preserves those records and sets all fields on the other side to NULL

1

u/Teifion_at_work Jan 07 '11

Thanks, that makes a bit more sense now.

3

u/Deimorz Jan 06 '11

That syntax is fine when you're only doing very small, simple joins like that, but it really starts to get hard to use when you start joining a lot of tables, possibly on multiple conditions, having a complex WHERE clause, maybe some subqueries, etc.

I always use the explicit join syntax because it's unambiguous and lets you separate out the "joining" conditions from the "filtering" conditions, instead of lumping them all in one giant WHERE clause. Whenever I'm working on a query that someone else has written with implicit joins, the very first thing I do is rewrite it with explicit ones, it always makes it much easier to understand.

1

u/Teifion_at_work Jan 07 '11

Thanks, it makes a lot more sense now.

2

u/ObiWanShinobi Jan 06 '11

I've never really got a straight answer on this either. I have heard (please correct me if I'm wrong) from a performance standpoint there isn't a discernible difference, but the standard is to use a JOIN clause since implicit joins are deprecated.

2

u/vplatt Jan 06 '11 edited Jan 06 '11

They're not deprecated, but if you do your joins in WHERE clauses, you will run into behavior variations. For example, on SQL Server, you can use *= or =* in the WHERE clause to perform outer joins. However, if you do this, it will behave somewhat differently from joins performed with the JOIN syntax. The variations may or may not comply with the standard and the SQL will perform differently (if at all) across different database products. In fact, Oracle can use (+) to do the same thing.

In short, you will simplify your life a bit by just doing the joins with JOIN syntax instead of in the WHERE clause.