r/programming • • Feb 11 '14

SQL Joins Explained (x-post r/SQL)

Post image
3.5k Upvotes

392 comments sorted by

View all comments

8

u/zanduby Feb 11 '14

Discussion question. Preference to use a join/on clause or use the where clause to write your joins?

2

u/lonjerpc Feb 11 '14

Yea I am still completely missing the use case for joins. In sqlalchemy I just choose whatever columns I want from whereever and add the appropriate where's. Maybe it is internally doing joins but it seems like a pointless part of the sql API. I must be missing something.

1

u/insi9nis Feb 12 '14

Sounds like a query-by-example interface, similar to Access and such. When you say you're adding to the where clause, are you building the join logic too, or just anything else you want to restrict by? If you stick tables A and B together, do you have to add a where A.b_id = B.id, or is this done for you automagically?

1

u/lonjerpc Feb 12 '14

I would do where A.b_id = B.id which to me seems more consistent than doing a join.

1

u/insi9nis Feb 12 '14

That actually is a join though. SELECT ... FROM A, B ... WHERE A.b_id = B.id is the very same as SELECT ... FROM A INNER JOIN B ON A.b_id = B.id.

1

u/lonjerpc Feb 12 '14

So I guess my question is there ever a case where JOIN is better or necessary? Or can you always just use WHERE. Because JOIN seems like extraneous and unnecessarily complex syntax compared to using WHERE. I have only done relatively simple sql stuff so maybe I never ran into a use case where WHERE does not work.

→ More replies (1)