INNER JOIN in SQL
INNER JOIN in SQL
The INNER JOIN is one of the most commonly used SQL joins. It allows you to retrieve data from two or more tables based on a matching condition. If the matching value exists in both tables, the INNER JOIN returns the row.
In this beginner-friendly tutorial, you'll learn:
What INNER JOIN is
How INNER JOIN works
Syntax of INNER JOIN
Real-world examples
INNER JOIN with multiple tables
Best practices
The INNER JOIN keyword selects records that have matching values in both tables involved in the join.
β Only rows common to both tables are returned.
β Rows without matching values are excluded.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
users table| id | name | city |
|---|---|---|
| 1 | Amit | Delhi |
| 2 | Neha | Mumbai |
| 3 | Rahul | Bengaluru |
orders table| id | user_id | product |
|---|---|---|
| 1 | 1 | Mobile |
| 2 | 3 | Laptop |
| 3 | 5 | Tablet |
Get all users who have placed an order:
SELECT users.name, orders.product
FROM users
INNER JOIN orders
ON users.id = orders.user_id;
| name | product |
|---|---|
| Amit | Mobile |
| Rahul | Laptop |
The row with user_id = 5 in orders is ignored because no such user exists.
SELECT u.name, u.city, o.product
FROM users AS u
INNER JOIN orders AS o
ON u.id = o.user_id;
Get all orders made by users from Mumbai:
SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
WHERE u.city = 'Mumbai';
SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
AND o.product = 'Laptop';
Example tables: users, orders, and payments.
SELECT u.name, o.product, p.amount
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
INNER JOIN payments p
ON o.id = p.order_id;
Aliases make queries shorter:
SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id;
SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id;
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.id;
SELECT s.name, c.course_name
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;
β Always use clear join conditions
β Prefer aliases (u, o, e) for readability
β Avoid ambiguous column names (use table prefixes)
β Index the joined columns for better performance
β Use meaningful WHERE clauses to reduce result size
In this tutorial, you learned:
What INNER JOIN is
How INNER JOIN matches rows between tables
Syntax and usage patterns
Real-world examples from e-commerce, HR, and school databases
Best practices for writing efficient JOIN queries
INNER JOIN is essential for combining relational data across multiple tables.