Part 1: Understanding SQL Joins – Bringing Tables Together
When working with relational databases, data often resides across multiple tables. That's where joins come into play! Joins allow us to combine data from two or more tables based on related columns, unlocking the full potential of our data. In this first part, we’ll cover the essentials and explore inner joins and left joins.
What is a SQL Join?
A join is simply a way to link rows from different tables based on a related column. Think of it like building a bridge between two tables—bringing together the data that belongs together.
Types of Joins
While there are multiple types of joins, in this blog, we’ll focus on two common ones:
Inner Join: Retrieves matching rows between two tables.
Left Join: Retrieves all rows from the left table and matching rows from the right table (with unmatched rows filled with
NULL
).
1. Inner Join
An inner join returns rows where the join condition matches in both tables. If there’s no match, the row doesn’t make it to the result set.
Example: Getting Users with Orders
Imagine we have two tables:
users
: Contains user information.orders
: Stores the orders each user has placed.
SELECT users.username, orders.order_id
FROM users
INNER JOIN orders ON users.id = orders.user_id;
The query joins the
users
andorders
tables where theid
fromusers
matchesuser_id
inorders
.Only users who have matching orders are included in the results.
2. Left Join
A left join (also called left outer join) ensures that all rows from the left table are included, even if there’s no matching row in the right table. If no match is found, the result will show NULL
for columns from the right table.
SELECT users.username, orders.order_id
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
The query retrieves all users from the
users
table.For users who haven’t placed any orders, the
order_id
column will showNULL
.
This type of join is useful when we want to ensure that no data from the left table is missed. For example, if we’re generating a report of all users, including those who haven’t ordered yet.
Comparing Inner and Left Joins
Join Type | What it Returns | When to Use |
Inner Join | Only matching rows from both tables | When we care only about connected data |
Left Join | All rows from the left table + matching rows | When we need everything from the left |
Conclusion
In this first part, we’ve explored the inner join and left join, learning how they connect rows from two tables based on shared columns. Knowing when to use these joins is crucial. Inner joins are great when we’re only interested in matching data, while left joins ensure we capture all the data from the left table, even if some rows don’t have matches.
Subscribe to my newsletter
Read articles from Akshobya KL directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Akshobya KL
Akshobya KL
Full stack developer dedicated to crafting seamless user experiences. I thrive on transforming complex problems into elegant solutions!