INNER JOIN in SQL

11/22/2025

INNER JOIN in SQL

Go Back

INNER JOIN in SQL – SQL Tutorial for Beginners

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


INNER JOIN in SQL

🔹 What Is INNER JOIN?

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.


🔸 INNER JOIN Syntax

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

🔸 Example Tables

👤 users table

idnamecity
1AmitDelhi
2NehaMumbai
3RahulBengaluru

📦 orders table

iduser_idproduct
11Mobile
23Laptop
35Tablet

🔸 Basic INNER JOIN Example

Get all users who have placed an order:

SELECT users.name, orders.product
FROM users
INNER JOIN orders
ON users.id = orders.user_id;

✔ Output:

nameproduct
AmitMobile
RahulLaptop

The row with user_id = 5 in orders is ignored because no such user exists.


🔸 INNER JOIN with Selected Columns

SELECT u.name, u.city, o.product
FROM users AS u
INNER JOIN orders AS o
ON u.id = o.user_id;

🔸 INNER JOIN with WHERE Clause

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';

🔸 INNER JOIN with Multiple Conditions

SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
AND o.product = 'Laptop';

🔸 INNER JOIN with Three Tables

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;

🔸 INNER JOIN with Aliases

Aliases make queries shorter:

SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id;

🔹 Real-World Examples

✔ E-commerce: Users with orders

SELECT u.name, o.product
FROM users u
INNER JOIN orders o
ON u.id = o.user_id;

✔ HR System: Employees with department details

SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.id;

✔ School System: Students with enrolled courses

SELECT s.name, c.course_name
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;

🔹 Best Practices for INNER JOIN

✔ 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


Summary

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.