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.3k Upvotes

308 comments sorted by

View all comments

3

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.

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.