Joining tables is one of the most common tasks in SQL. Whether you’re building a report or writing a backend query, you’ll often need to combine data from multiple tables. In this article, we’ll walk you through how to inner join 3 tables in SQL step by step, using real examples.
If you’ve mastered joining two tables, you’re already halfway there. Let’s now look at how to inner join multiple tables, especially three tables, using simple syntax and explanations.
An INNER JOIN returns only the rows where there is a match in all the tables involved. It excludes rows that don’t have a corresponding match in the other table(s).
When joining 3 tables, an inner join ensures that only the rows present in all three tables based on the join conditions are returned.
Here’s the basic syntax for inner joining 3 tables:
SELECT
table1.column1,
table2.column2,
table3.column3
FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column
INNER JOIN table3 ON table2.another_column = table3.another_column;
Let’s now look at a real-world example.
Let’s create three tables: customers
, products
, and orders
. Then we’ll insert sample data and use an INNER JOIN to combine them. This will demonstrate how to inner join 3 tables with real data.
1- Create the Tables
-- Create customers table
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100)
);
-- Create products table
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10, 2)
);
-- Create orders table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
product_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
2- Insert Sample Data
-- Insert into customers
INSERT INTO customers (customer_id, customer_name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');
-- Insert into products
INSERT INTO products (product_id, product_name, price) VALUES
(101, 'Laptop', 1000.00),
(102, 'Smartphone', 600.00),
(103, 'Tablet', 400.00);
-- Insert into orders
INSERT INTO orders (order_id, customer_id, product_id, order_date) VALUES
(1001, 1, 101, '2024-01-10'), -- Alice bought a Laptop
(1002, 2, 102, '2024-02-15'), -- Bob bought a Smartphone
(1003, 1, 103, '2024-03-01'); -- Alice bought a Tablet
3- Query to Inner Join 3 Tables
Now let’s inner join all three tables to get a full picture of the orders.
SELECT
customers.customer_name,
products.product_name,
products.price,
orders.order_date
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id
INNER JOIN products ON orders.product_id = products.product_id;
- We’re selecting data from all three tables: customer name, product name, price, and order date.
- The joins happen:
orders.customer_id = customers.customer_id
orders.product_id = products.product_id
- We only get rows where matches exist in all three tables.
4- Expected Results:
customer_name | product_name | price | order_date |
---|---|---|---|
Alice | Laptop | 1000.00 | 2024-01-10 |
Bob | Smartphone | 600.00 | 2024-02-15 |
Alice | Tablet | 400.00 | 2024-03-01 |
- Order matters: The sequence of your joins should make logical sense.
- Use table aliases for better readability if your queries get long.
- Always check join keys: Make sure the columns you’re joining on actually match.
- Avoid ambiguous column names: Qualify them with table names if needed.
Forgetting a join condition is a common mistake. If you forget to join one of the tables properly, you may get a Cartesian product, where each row is combined with every other row, leading to a huge, incorrect result set.
Yes, the same idea! You can inner join multiple tables — 4, 5, or more — using the same syntax:
FROM table1
INNER JOIN table2 ON ...
INNER JOIN table3 ON ...
INNER JOIN table4 ON ...
Just be careful to manage the logic and performance as the joins increase.
Inner joining 3 tables in SQL is straightforward once you understand how joins work. By using clear join conditions and matching keys, you can combine data from multiple tables into one useful result. Practice with real data to build confidence and avoid common mistakes.
If you found this article useful, you can check more:
- Isolation Levels In SQL Server With Examples
- Group by Year, Month, or Day in PostgreSQL
- Open-Closed Principle: The Hard Parts
Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.