SQL Joins — Introduction
Combine data from two or more tables
✅ What You Will Learn
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_id | customer_name | city | | | order_id | customer_id | product | amount |
|---|---|---|---|---|---|---|---|
| 1 | Rahul Sharma | Delhi | | | 1001 | 1 | Laptop | 45000 |
| 2 | Priya Verma | Noida | | | 1002 | 2 | Mobile Phone | 18000 |
| 3 | Amit Kumar | Gurgaon | | | 1003 | 1 | Headphones | 3500 |
| 4 | Sneha Kapoor | Delhi | | | 1004 | 4 | Laptop | 52000 |
| 5 | Vikram Singh | Noida | | | 1005 | 5 | Tablet | 28000 |
Example
📌 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
❓ 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.
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.