Preloader

Joining Multiple Tables in SQL

SQL
SQL Tutorials

Joining Multiple Tables in SQL enables you to combine data from different tables into a single meaningful result. In most real-world databases, data is organized in multiple tables. This means that you have to join the tables to create reports, view data, and answer business questions.

In most of your real-world projects, you will be joining three or more tables.

Why Do We Join Multiple Tables?

Good database design prevents the problem of repeating data. Therefore, we break down information into different tables.

Example:

  • Customers table holds customer information
  • Orders table holds order information
  • Products table holds product information

Each table has a different function. To view complete order information, you have to join them.

Example Tables

Customers

customer_idcustomer_name
1Ram
2Vedika

Orders

order_idcustomer_idproduct_id
101118
102222

Products

product_idproduct_name
18Laptop
22Mobile

The tables connect using keys:

  • customer_id links Customers and Orders
  • product_id links Orders and Products

Joining Three Tables

You can join them using INNER JOIN.

SELECT 
    customers.customer_name,
    products.product_name,
    orders.order_id
FROM orders
INNER JOIN customers 
    ON orders.customer_id = customers.customer_id
INNER JOIN products 
    ON orders.product_id = products.product_id;

This query works in steps.

First, SQL joins Orders with Customers.
In Next, it joins that result with Products.
Finally, it shows the selected columns.

Result

customer_nameproduct_nameorder_id
RamLaptop101
vedikaMobile102

Use Aliases to Keep It Simple

Long table names make queries harder to read. So, use aliases in your queries

SELECT 
    c.customer_name,
    p.product_name,
    o.order_id
FROM orders o
INNER JOIN customers c 
    ON o.customer_id = c.customer_id
INNER JOIN products p 
    ON o.product_id = p.product_id;

This version is shorter and cleaner.

Using LEFT JOIN with Multiple Tables

Sometimes you want all customers, even if they have no orders. In that case, use LEFT JOIN.

SELECT 
    c.customer_name,
    p.product_name,
    o.order_id
FROM customers c
LEFT JOIN orders o 
    ON c.customer_id = o.customer_id
LEFT JOIN products p 
    ON o.product_id = p.product_id;

Now, customers without orders will still appear. The order fields will show NULL.

Then, write JOIN conditions carefully. With practice, multi table joins become easy to understand and write.

Important Tips

Always include an ON condition for every JOIN

Join the correct columns

Do not use SELECT * in complex queries

Index foreign keys

Test your query on sample data

Common Mistakes

Missing ON condition

Joining the wrong columns

Using INNER JOIN instead of LEFT JOIN

Ignoring NULL values

Small errors can produce different results.

Final Thoughts

Joining multiple tables in SQL allows you to combine multiple pieces of data. First, establish the relationship between tables.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *