· 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.

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:

  1. Only shows books that have authors
  2. Only shows authors that have books
  3. 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:

  1. Shows IDs from both tables
  2. Displays only matching records
  3. 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:

  1. First matches books and authors
  2. Then filters based on price
  3. 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

  1. Table Aliases

    • Use short, meaningful aliases
    • Example: books as b, authors as a
    • Be consistent throughout your query
  2. Join Conditions

    • Always specify the exact columns to match
    • Make sure data types match
    • Example: ON b.book_id = a.book_id
  3. Column Selection

    • Use table aliases for all columns
    • Be specific about which columns you need
    • Example: b.title, a.author_name
  4. Query Organization

    • List the main table first
    • Keep join conditions simple
    • Use proper indentation
  5. 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:

  1. Forgetting table aliases
  2. Not specifying which table columns come from
  3. Using wrong columns in join condition
  4. Expecting to see unmatched records

Happy querying! 🚀

Back to Blog

Related Posts

View All Posts »