SQL joins can seem intimidating at first, but they’re one of the most powerful tools in your data toolbox. They let you combine data from multiple tables based on related columns — unlocking insights that live across your database.
What is Join in SQL?
A JOIN connects rows from two or more tables based on a related column — usually a foreign key. Think of it as forming meaningful conversations between tables.
Customer Table
id | name | country |
1 | Alice | USA |
2 | Bob | Canada |
3 | Charlie | UK |
4 | Diana | Australia |
5 | Ethan | Germany |
Orders Table
id | customer_id | product | amount |
101 | 1 | Laptop | 1200 |
102 | 1 | Headphone | 150 |
103 | 2 | Smartphone | 800 |
104 | 4 | Monitor | 300 |
105 | 6 | Keyboard | 100 |
4 Most common SQL JOINs
1. Inner JOIN
Returns only matching rows from both tables.
SELECT * FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
Use it when: You want data only where both side have a match
2. LEFT JOIN (LEFT OUTER JOIN)
Return all rows from the left table, and matched rows from the right. Null if no match.
SELECT * FROM customers
LEFT JOIN orders ON customer.id = orders.customer_id;
Use it when: You want to keep everything from the left table.
3. RIGHT JOIN (RIGHT OUTER JOIN)
Returns all rows from the right table, and matched rows from the left.
SELECT * FROM orders
RIGHT JOIN customers ON orders.customer_id = customers.id;
Use it when: You want to keep everything from the right table.
4. FULL JOIN (FULL OUTER JOIN)
Returns all records when there is a match in either table. Nulls where there’s no match.
SELECT * FROM customers
FULL JOIN orders ON customers.id = orders.customer_id;
Pro Tips: Use Aliases for cleaner SQL
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;
Leave a Reply