โ INNER JOIN
โ Returns only matching rows from both tables
๐งฉ Think: Intersection
Example:
SELECT *
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
โ LEFT JOIN (LEFT OUTER JOIN)
โ All rows from left table + matching from right (NULL if no match)
๐ Think: All from Left, matching from Right
Example:
SELECT *
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;
โ RIGHT JOIN (RIGHT OUTER JOIN)
โ All rows from right table + matching from left (NULL if no match)
๐งญ Think: All from Right, matching from Left
Example:
SELECT *
FROM orders
RIGHT JOIN customers ON orders.customer_id = customers.id;
โ FULL JOIN (FULL OUTER JOIN)
โ All rows from both tables, matching where possible
๐ Think: Union of both
Example:
SELECT *
FROM customers
FULL OUTER JOIN orders ON customers.id = orders.customer_id;
โ CROSS JOIN
โ Cartesian product of every row in A ร every row in B
โพ๏ธ Use carefully!
Example:
SELECT *
FROM colors
CROSS JOIN sizes;
โ SELF JOIN
โ Join a table to itself using aliases
๐ Useful for hierarchical data
Example:
SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
LEFT JOIN employees e2 ON e1.manager_id = e2.id;
๐ก Remember: Use
JOIN ON common_column to link tables correctly!Double Tap โฅ๏ธ For More