INNER JOIN in SQL

11/22/2025
All Articles

INNER JOIN in SQL

INNER JOIN in SQL

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


πŸ”Ή 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

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

πŸ”Έ 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:

name product
Amit Mobile
Rahul Laptop

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.

Article