TutorialsSQLSQL Joins — Introduction
🟢 Free Demo
SQL TutorialTopic 10 of 20

SQL Joins — Introduction

Combine data from two or more tables

✅ What You Will Learn

Why relational databases split data into multiple tables
What a JOIN does and why it is needed
The concept of primary key and foreign key
The four types of SQL JOINs: INNER, LEFT, RIGHT, FULL
Which JOIN type to use in different situations

In real databases, data is split across multiple tables. Customer information is in one table, orders in another, products in a third. SQL JOINs combine these tables based on a matching column — called a key.

Understanding JOINs is what separates a beginner from a working data analyst. Over 80% of real-world SQL queries involve at least one JOIN.

There are four main types of JOIN: — INNER JOIN: only matching rows from both tables — LEFT JOIN: all rows from the left table, matched rows from the right — RIGHT JOIN: all rows from the right table, matched rows from the left — FULL OUTER JOIN: all rows from both tables

📋 customers table (left) + orders table (right) — linked by customer_id

customer_idcustomer_namecity|order_idcustomer_idproductamount
1Rahul SharmaDelhi|10011Laptop45000
2Priya VermaNoida|10022Mobile Phone18000
3Amit KumarGurgaon|10031Headphones3500
4Sneha KapoorDelhi|10044Laptop52000
5Vikram SinghNoida|10055Tablet28000

Example

ExampleTwo tables to work with
-- customers table
customer_id | customer_name | city
------------|---------------|--------
1           | Rahul Sharma  | Noida
2           | Priya Verma   | Delhi
3           | Amit Kumar    | Noida
4           | Sneha Kapoor  | Gurgaon

-- orders table
order_id | customer_id | product      | amount
---------|-------------|--------------|-------
1001     | 1           | Laptop       | 45000
1002     | 2           | Mobile Phone | 18000
1003     | 1           | Headphones   | 3500
1004     | 5           | Tablet       | 22000
💡

customer_id is the key that links the two tables. Notice customer_id 5 in orders has no matching customer in the customers table — this is where JOIN types matter.

📌 Key Points to Remember

  • JOINs combine rows from two or more tables based on a matching column
  • The matching column is called a key — usually an ID column
  • Always specify which table each column comes from using table.column notation
  • INNER JOIN is the most common JOIN in analytics
  • LEFT JOIN is the second most common — it keeps all rows from the left table

🏢 Real-World Application

In real company databases, data is never stored in one big table. A retail company stores customers in a customers table, orders in an orders table, products in a products table, and payments in a payments table. Every report that combines customer information with order information requires a JOIN. "Which customers from Delhi placed orders above ₹50,000 this quarter?" needs the customers table JOINed with the orders table. Understanding JOINs is often the dividing line between junior and mid-level data analyst roles.

⚠️ Common Mistakes to Avoid

WRONGConfusing which table to start from (left vs right)
FIXThink about which table has ALL the rows you want to preserve. Put that table first (the "left" table). If you want all orders even with no matching customer, orders is your left table.
WRONGForgetting to specify the ON condition
FIXEvery JOIN must have an ON clause that specifies which columns link the two tables. Missing ON causes a Cartesian product — every row of table A multiplied by every row of table B (disastrous on large tables).

❓ Frequently Asked Questions

What is a JOIN in SQL?

A JOIN combines rows from two or more tables based on a related column between them. For example, you can JOIN an orders table with a customers table on customer_id to get customer details alongside each order.

What are the types of SQL JOINs?

The main JOIN types are: INNER JOIN (returns only rows with matches in both tables), LEFT JOIN (all rows from left table + matching rows from right), RIGHT JOIN (all rows from right table + matching rows from left), and FULL OUTER JOIN (all rows from both tables).

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows where there is a match in both tables — unmatched rows are dropped. LEFT JOIN returns all rows from the left table; where there is no match in the right table, it fills with NULL.

What is a primary key and foreign key in SQL?

A primary key is a unique identifier for each row in a table (e.g., customer_id in the customers table). A foreign key is a column in another table that references that primary key (e.g., customer_id in the orders table). JOINs use these to link tables.

How many tables can you JOIN in one SQL query?

There is no hard limit. You can chain multiple JOINs: FROM orders JOIN customers ON ... JOIN products ON ... JOIN payments ON ... However, more JOINs increase complexity and query time, so join only the tables you need.

✏️ Practice Exercise

Look at the two tables above. Which customer placed order 1003? To answer this, your query would need to JOIN the two tables. We will write that query in the next topic.

← PreviousHAVING ClauseNext →INNER JOIN
🎓 Level Up Faster

Learn SQL with Live Trainer Guidance

These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.