Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the uses of the different join operations?

Tags:

sql

rdbms

What are the uses of the different join operations in SQL? Like I want to know why do we need the different inner and outer joins?

like image 648
varunthacker Avatar asked Apr 11 '10 13:04

varunthacker


People also ask

What is the use of join operator?

The JOIN operator specifies how to relate tables in the query. Result of applying these joins in a query: INNER JOIN: Select only those rows that have values in common in the columns specified in the ON clause.

What are the different types of join operations in SQL?

Different Types of SQL JOINs (INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.


2 Answers

The only type of join you really need is LEFT OUTER JOIN. Every other type of join can be rewritten in terms of one or more left outer joins, and possibly some filtering. So why do we need all the others? Is it just to confuse people? Wouldn't it be simpler if there were only one type of join?

You could also ask: Why have both a <= b and b >= a? Don't these just do the same thing? Can't we just get rid of one of them? It would simplify things!

Sometimes it's easier to swap <= to >= instead of swapping the arguments round. Similarly, a left join and a right join are the same thing just with the operands swapped. But again it's practical to have both options instead of requiring people to write their queries in a specific order.

Another thing you could ask is: In logic why do we have AND, OR, NOT, XOR, NAND, NOR, etc? All these can be rewritten in terms of NANDs! Why not just have NAND? Well it's awkward to write an OR in terms of NANDs, and it's not as obvious what the intention is - if you write OR, people know immediately what you mean. If you write a bunch of NANDs, it is not obvious what you are trying to achieve.

Similarly, if you want to do a FULL OUTER JOIN b you could make a left join and a right join, remove duplicated results, and then union all. But that's a pain and so there's a shorthand for it.

When do you use each one? Here's a simplified rule:

  • If you always want a result row for each row in the LEFT table, use a LEFT OUTER JOIN.
  • If you always want a result row for each row in the RIGHT table, use a RIGHT OUTER JOIN.
  • If you always want a result row for each row in either table, use a FULL OUTER JOIN.
  • If you only want a result row when there's a row in both tables, use an INNER JOIN.
  • If you want all possible pairs of rows, one row from each table, use a CROSS JOIN.
like image 199
Mark Byers Avatar answered Sep 20 '22 21:09

Mark Byers


inner join - joins rows from both sets of the match based on specified criteria.

outer join - selects all of one set, along with matching or empty (if not matched) elements from the other set. Outer joins can be left or right, to specify which set is returned in its entirety.

like image 21
davek Avatar answered Sep 20 '22 21:09

davek