SQL Joins Explained

0
SQL Joins Explained

SQL Joins Explained with an Easy Analogy!

SQL Joins Diagram

Imagine you have two baskets of fruit:

  • Basket A has apples, bananas, and oranges.
  • Basket B has bananas, oranges, and grapes.

We are going to look at different ways we can combine these baskets using SQL JOINs.


1. INNER JOIN

This is like saying, "Give me the fruits that are in both baskets."


Result: Only the fruits that appear in both baskets (Bananas and Oranges).


Analogy: "I only want the fruits that are in both baskets."


Fruit
Banana
Orange

2. LEFT JOIN (or LEFT OUTER JOIN)

This is like saying, "Give me all the fruits from Basket A, and if they also appear in Basket B, give me that too."


Result: All fruits from Basket A, even if they don't appear in Basket B. If they are also in Basket B, you’ll see them.


Analogy: "I want everything from Basket A, and if they match in Basket B, give me that too."


Fruit Basket B Match
AppleNULL
BananaBanana
OrangeOrange

3. RIGHT JOIN (or RIGHT OUTER JOIN)

This is like saying, "Give me all the fruits from Basket B, and if they also appear in Basket A, give me that too."


Result: All fruits from Basket B, even if they don’t appear in Basket A. If they are also in Basket A, you’ll see them.


Analogy: "I want everything from Basket B, and if they match in Basket A, give me that too."


Fruit Basket A Match
BananaBanana
OrangeOrange
GrapeNULL

4. FULL JOIN (or FULL OUTER JOIN)

This is like saying, "Give me everything from both baskets, even if they don't match."


Result: All fruits from both baskets. If they don't match, you'll still see them, but with NULL where there's no match.


Analogy: "I want everything from both baskets, even if they don’t match."


Fruit Basket A Match Basket B Match
AppleAppleNULL
BananaBananaBanana
OrangeOrangeOrange
GrapeNULLGrape

Quick Recap:

  • INNER JOIN: Only the matching fruits (from both baskets).
  • LEFT JOIN: Everything from Basket A, with matches from Basket B.
  • RIGHT JOIN: Everything from Basket B, with matches from Basket A.
  • FULL JOIN: Everything from both baskets, with NULL where there’s no match.

Post a Comment

0Comments

Thanks for your feedback

Post a Comment (0)