JOINS let you combine data from two or more tables based on related columns.
1๏ธโฃ INNER JOIN
Returns only matching rows from both tables.
Problem: Get customers with their orders.
SELECT c.name, o.order_id
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;
โ Only shows customers who have orders.
2๏ธโฃ LEFT JOIN (or LEFT OUTER JOIN)
Returns all rows from the left table + matching rows from the right table. If no match, fills with NULL.
Problem: Show all customers, even if they didnโt order.
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;
โ Includes customers without orders.
3๏ธโฃ RIGHT JOIN
Opposite of LEFT JOIN: keeps all rows from the right table.
4๏ธโฃ FULL OUTER JOIN
Returns all rows from both tables. Where thereโs no match, it shows NULL.
SELECT c.name, o.order_id
FROM customers c
FULL OUTER JOIN orders o ON c.id = o.customer_id;
โ Includes customers with or without orders and orders with or without customers.
5๏ธโฃ SELF JOIN
Table joins with itself.
Problem: Show employees and their managers.
SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.id;
โ Links each employee to their manager using a self join.
๐ก Quick Summary:
โข INNER JOIN โ Only matches
โข LEFT JOIN โ All from left + matches
โข RIGHT JOIN โ All from right + matches
โข FULL OUTER JOIN โ Everything
โข SELF JOIN โ Table joins itself
๐ฌ Tap โค๏ธ for more!