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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 <= bandb >= 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 ofNANDs! Why not just haveNAND? Well it’s awkward to write anORin terms ofNANDs, and it’s not as obvious what the intention is – if you writeOR, people know immediately what you mean. If you write a bunch ofNANDs, it is not obvious what you are trying to achieve.Similarly, if you want to do
a FULL OUTER JOIN byou 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: