· SQL Guroo · SQL Tutorial · 3 min read
Mastering SQL Joins - Understanding INNER JOIN
Learn how to use INNER JOIN in SQL with simple, practical examples.
Introduction
INNER JOIN is like finding matches between two lists. It only shows records that exist in both tables. Think of it as finding the common elements between two sets.
Why INNER JOIN Matters?
It’s useful when you need to:
- Match customers with their orders
- Find students and their grades
- Connect products with their categories
- See only the data that exists in both tables
Main Content: Understanding INNER JOIN
Let’s use these simple tables:
-- Create two simple tables
CREATE TABLE books (
book_id INT,
title VARCHAR(50),
price DECIMAL(5,2)
);
CREATE TABLE authors (
author_id INT,
book_id INT,
author_name VARCHAR(50)
);
-- Insert sample data
INSERT INTO books VALUES
(1, 'SQL Basics', 29.99),
(2, 'Python Fun', 24.99),
(3, 'Java Guide', 34.99),
(4, 'Web Design', 19.99);
INSERT INTO authors VALUES
(101, 1, 'John Smith'),
(102, 2, 'Sarah Johnson'),
(103, 3, 'Mike Wilson'),
(104, 5, 'Lisa Brown');
Example 1: Basic INNER JOIN
Let’s match books with their authors:
SELECT
b.title,
b.price,
a.author_name
FROM books b
INNER JOIN authors a
ON b.book_id = a.book_id;
This simple query:
- Only shows books that have authors
- Only shows authors that have books
- Matches them based on book_id
Results:
title | price | author_name
-----------|--------|-------------
SQL Basics | 29.99 | John Smith
Python Fun | 24.99 | Sarah Johnson
Java Guide | 34.99 | Mike Wilson
Notice that:
- ‘Web Design’ isn’t shown (no matching author)
- ‘Lisa Brown’ isn’t shown (no matching book)
Example 2: Multiple Column Selection
Let’s see all IDs along with the details:
SELECT
b.book_id,
b.title,
a.author_id,
a.author_name
FROM books b
INNER JOIN authors a
ON b.book_id = a.book_id;
This query:
- Shows IDs from both tables
- Displays only matching records
- Helps understand the relationships
Results:
book_id | title | author_id | author_name
--------|------------|-----------|-------------
1 | SQL Basics | 101 | John Smith
2 | Python Fun | 102 | Sarah Johnson
3 | Java Guide | 103 | Mike Wilson
Example 3: Adding Simple Conditions
Let’s find books and authors where the book price is over $25:
SELECT
b.title,
b.price,
a.author_name
FROM books b
INNER JOIN authors a
ON b.book_id = a.book_id
WHERE b.price > 25.00;
This query:
- First matches books and authors
- Then filters based on price
- Shows only the matching expensive books
Results:
title | price | author_name
-----------|-------|-------------
SQL Basics | 29.99 | John Smith
Java Guide | 34.99 | Mike Wilson
Best Practices
Table Aliases
- Use short, meaningful aliases
- Example: books as b, authors as a
- Be consistent throughout your query
Join Conditions
- Always specify the exact columns to match
- Make sure data types match
- Example: ON b.book_id = a.book_id
Column Selection
- Use table aliases for all columns
- Be specific about which columns you need
- Example: b.title, a.author_name
Query Organization
- List the main table first
- Keep join conditions simple
- Use proper indentation
Data Understanding
- Remember INNER JOIN only shows matches
- No NULL values in join columns
- Both tables must have matching data
Remember:
- INNER JOIN shows only matching records
- Records without matches are excluded
- It’s perfect when you only want complete data
- Both tables must have the connecting column
Common Mistakes to Avoid:
- Forgetting table aliases
- Not specifying which table columns come from
- Using wrong columns in join condition
- Expecting to see unmatched records
Happy querying! 🚀