🔗 SQL JOINS – How to Combine Data from Multiple Tables

(because real-world stuff lives in more than one place)

👀 Picture This…

You’ve got two tables:

🧑 users table:

user_idname
1Olivia
2Mason
3Zoe

📦 orders table:

order_iduser_idproduct
1011Laptop
1021Headphones
1032Phone

Now the question is:
“What did each user buy?”
But that info is split across two tables.

That’s where JOIN comes in.

🔧 Basic Syntax (INNER JOIN)

SELECT users.name, orders.product
FROM users
JOIN orders
ON users.user_id = orders.user_id;

You’re telling SQL:
“Hey, connect these two tables where their user_id matches.”

✅ Result:

nameproduct
OliviaLaptop
OliviaHeadphones
MasonPhone

🔁 Types of JOINS (The Quick Guide)

TypeWhat it shows
INNER JOINOnly matched rows from both tables
LEFT JOINAll rows from the left table + matches from the right
RIGHT JOINAll rows from the right table + matches from the left
FULL JOINAll rows from both tables (if supported)

🧪 Example: LEFT JOIN

Let’s see all users, even if they haven’t ordered anything yet:

SELECT users.name, orders.product
FROM users
LEFT JOIN orders
ON users.user_id = orders.user_id;

✅ Result:

nameproduct
OliviaLaptop
OliviaHeadphones
MasonPhone
ZoeNULL

LEFT JOIN keeps everyone from users, even if there’s nothing in orders.

🧃 Recap

  • Use JOIN to combine related data across tables
  • ON links them through matching columns (like user_id)
  • INNER JOIN = only matched rows
  • LEFT JOIN = keep all from the left
  • JOINs let you answer real questions, like “who bought what?”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top