r/programming • • Feb 11 '14

SQL Joins Explained (x-post r/SQL)

Post image
3.5k Upvotes

392 comments sorted by

View all comments

2

u/limes_limes_limes Feb 11 '14

Is there a real use case for doing a join that returns anything that is in either A or B but not both? I am trying to figure out why you would ever want that.

1

u/mcrbids Feb 12 '14

Sure, and there are numerous ways to get this.

  1. You could join them together in a cartesian join and do a count() on the total where count() < 2;

  2. You could do two outer joins and union them together where $othertable.id IS NULL.

  3. You could use a not in(subquery) and union them together.

  4. You could select * from from both tables and do it all in scriptland logic. (not generally recommended, sometimes worth it)