Joining tables is key in relational databases. Learn how to do it right with this guide.
If you're working with relational databases, mastering JOIN
operations in SQL is essential. Joins let you combine data from two or more tables based on a related column between them. Whether you're exploring user behavior, sales transactions, or marketing performance, you'll almost always need to connect tables together.
This guide will walk you through the different types of SQL joins, when to use them, and how to write them clearly—perfect for beginners or anyone needing a quick refresher.
Relational databases store data in separate tables to avoid redundancy. JOINs let you pull that related data together in a single query.
You have a users
table with user info and an orders
table with purchases. To see what each user bought, you need to join those two tables using a common column—like user_id
.
JOINs give you the power to:
You can test these JOINs in a modern, browser-based SQL environment using Galaxy’s free SQL editor.
SQL offers several types of joins, each serving a different purpose depending on the relationship and data coverage you need.
Returns only rows that have matching values in both tables.
SELECT users.name, orders.total
FROM users
INNER JOIN orders ON users.id = orders.user_id;
Use it when: You only want records with a match in both tables.
Returns all rows from the left table and matched rows from the right table. If there’s no match, the result is NULL on the right side.
SELECT users.name, orders.total
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
Use it when: You want all users, even if they haven’t placed an order yet.
Returns all rows from the right table, and the matched rows from the left table. NULLs fill in where there’s no match.
SELECT orders.total, users.name
FROM orders
RIGHT JOIN users ON users.id = orders.user_id;
Use it when: You want to see all orders—even if the user info is missing (less common).
Returns rows when there’s a match in either table. NULLs fill gaps.
Note: Not all databases support FULL OUTER JOIN (e.g., MySQL requires workarounds).
SELECT users.name, orders.total
FROM users
FULL OUTER JOIN orders ON users.id = orders.user_id;
Use it when: You want all records from both tables, with matches where available.
Using clear join conditions and table aliases helps simplify your queries.
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id;
Best practice: Always qualify your column names using aliases (u.name
, o.total
) to avoid ambiguity.
SELECT * FROM users JOIN orders;
-- This will fail or produce a Cartesian product
Always specify how the tables are related using ON
.
If both tables have a column like id
, SQL won’t know which one to use unless you specify the table or alias.
It’s better to specify only the columns you need—especially in joins, where duplicated column names can cause conflicts.
SELECT p.title, a.name
FROM posts p
JOIN authors a ON p.author_id = a.id;
SELECT e.timestamp, u.email
FROM events e
LEFT JOIN users u ON e.user_id = u.id;
Want to explore how JOINs work with actual datasets? Visit Galaxy's SQL Editor to try out queries on your own.
SELECT *
.u
, o
, etc.) to keep queries clean and readable.user_id
) for performance.JOINs are how relational databases come to life. Instead of duplicating information, you build lean, normalized tables—and then JOIN them when needed.
To recap:
INNER JOIN
for matches only.LEFT JOIN
when you want unmatched records from the left.FULL OUTER JOIN
when you want everything.ON
clause.If you’re just starting out or building a portfolio, mastering JOINs will take your SQL skills to the next level.
Try writing JOIN queries in the Galaxy SQL editor, a fast, AI-enabled workspace that makes querying structured data simple and powerful.
Continue learning: