MySQL — Inner Join
Basic INNER JOIN
SELECT * FROM users
INNER JOIN orders ON users.id = orders.user_id;
INNER JOIN with Aliases
SELECT u.name, o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id;
INNER JOIN Examples
-- Users with orders
SELECT
u.name,
COUNT(o.id) as order_count
FROM users u
INNER JOIN orders o ON u.id = o.user_id
GROUP BY u.id;
-- Products in orders
SELECT
p.name,
SUM(oi.quantity) as total_sold
FROM products p
INNER JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id;
-- Multi-table join
SELECT
u.name,
p.name as product,
oi.quantity,
o.created_at
FROM users u
INNER JOIN orders o ON u.id = o.user_id
INNER JOIN order_items oi ON o.id = oi.order_id
INNER JOIN products p ON oi.product_id = p.id;
INNER JOIN vs WHERE
-- These are equivalent
SELECT * FROM users
INNER JOIN orders ON users.id = orders.user_id;
SELECT * FROM users, orders
WHERE users.id = orders.user_id;
Mini Practice
Write MySQL code that:
- Uses basic INNER JOIN
- Joins with aliases
- Joins multiple tables
- Groups joined results
Up Next
Next: Learn about Left Join.
Related Topics
Frequently Asked Questions about Inner Join
What is Inner Join in MySQL?
Inner Join is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Inner Join?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Inner Join.
Why is Inner Join important in MySQL?
Inner Join is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.