What is it?
A JOIN combines rows from two or more tables based on a related column -- usually a foreign key. This is how normalised data is brought together for queries.
Why does it matter?
Databases split data into separate tables to avoid repetition. JOINs bring it back together. Understanding JOINs is one of the most important and most tested SQL skills.
Understand INNER JOIN, LEFT JOIN, multi-table JOINs, and finding rows with no match.
Real-World Use Cases
- 🧾 Order history page - JOIN orders with users to show customer name alongside each order, and JOIN with products to show product names.
- 👤 User profile with posts - LEFT JOIN users with posts to show all users including those who have never written a post.
- 📊 Sales report - JOIN orders, users, and products in one query to build a complete sales report with all details.
- 🔍 Finding inactive users - LEFT JOIN users with orders WHERE orders.id IS NULL -- find users who have never placed an order.
INNER JOIN -- Only Matching Rows
SELECT u.name AS customer,
o.id AS order_id,
o.total
FROM users u
INNER JOIN orders o ON o.user_id = u.id;
-- Users with NO orders are excluded
-- Orders with no matching user are excluded
LEFT JOIN -- All Left Rows
SELECT u.name, o.total AS order_total
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;
-- Returns ALL users; NULL for order_total where user has no orders
-- Find users who NEVER ordered
SELECT u.name FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.id IS NULL;
Multi-Table JOIN
SELECT u.name AS customer,
p.name AS product,
o.quantity,
o.created_at
FROM orders o
JOIN users u ON u.id = o.user_id
JOIN products p ON p.id = o.product_id
WHERE o.created_at >= '2026-01-01'
ORDER BY o.created_at DESC
LIMIT 50;
Q: What is the difference between INNER JOIN and JOIN?
They are identical. JOIN is shorthand for INNER JOIN. Both return only rows where both sides have a matching value. Most developers write just JOIN for brevity.
Comments (0)
No comments yet. Be the first!
Leave a Comment