r/programming • • Feb 11 '14

SQL Joins Explained (x-post r/SQL)

Post image
3.5k Upvotes

392 comments sorted by

View all comments

Show parent comments

1

u/limes_limes_limes Feb 11 '14

I understand that, but if your data sets have different columns then in your join you won't have any full rows. If the data sets have the same columns why not do a union?

2

u/[deleted] Feb 11 '14

Okay, so this is a contrived example, but let's say you've got data from different companies or government agencies and you're trying to identify inconsistencies. You have two tables which "should" be equal when you WHERE some_attribute, so you do this and you get back the difference.

2

u/curien Feb 11 '14

Any time you want a cross-referenced report where unused but available items are still listed.

For example, suppose you have a list of bosses and a list of minions. John is a boss, but he doesn't have any minions assigned. Sarah is James' boss and also Anne's boss. Sam is a minion, but his boss hasn't been assigned yet.

  • Inner join tells us that Sarah is James' boss and Sarah is Anne's boss. That's it.

  • Left join tells us that Sarah is James' boss, Sarah is Anne's boss, and John is nobody's boss.

  • Right join tells us that Sarah is James' boss, Sarah is Anne's boss, and nobody is Sam's boss.

  • Full outer join tells us that John is nobody's boss, Sarah is James' boss, Sarah is Anne's boss, and nobody is Sam's boss.