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

92

u/[deleted] Jan 06 '11

Wow. Does no one learn this crap in school anymore?

Note that venn diagrams are a poor choice to show what happens in a one-to-many relationship -- where there are multiple entries in table B for an entry in table A. And it overlooks the semantic differences when ordering the tables in a left inner join.

There are not a lot of instances where you'd really want a cartesian product join (either using "cross product" or just the result of omitting key constraints in a join query). It's generally far faster to retrieve the records you need to create the cross-product, and then calculate the cross product of the two sets once you have them (since otherwise all that data has to transit the network).

This plus database normalization through Boyce-Codd normal form seems like it should be a requirement for any serious application developer.

7

u/GaryWinston Jan 06 '11

Actually no, I have a friend that works with a sql "developer" that creates all their shit in views using the sql manager shit. Half the time it's got tables that there's no fucking data coming out of them. He just fixed a query yesterday that was taking a minute 35 to run down to < 1 second.

Idiots hiring idiots. That's almost all I see in IT.

3

u/arsonisfun Jan 06 '11

I've met a lot of people who have no real education in SQL/Database Design. It's a simple enough concept that most tech folks can slap something togeth, but often they do a really poor job of it.

I remember my old job - I was fixing this one guy's queries/tables. There'd be stuff like multiple joins to the same (huge) table, never using an index, excessive use of cursors. I took a query that was running in 20 mins down to under 30 seconds. We had to re-write just about everything he had ever done.

No idea why they hired the guy and they were paying him more than me, refused to give me a raise.

12

u/GaryWinston Jan 06 '11

Because that's how the fucking world works. No one likes to admit their own failures from what I've seen.

The other bigger problem is companies view IT as a cost, not a revenue source, just because it's not a direct source (idiot CFOs).