SQL JOIN for Beginners Part 4: INNER JOIN

Suin LeeSuin Lee
3 min read

What is INNER JOIN?

INNER JOIN is a type of SQL join that retrieves only the common data (intersection) between two tables.

In other words, it returns only the rows that exist in both tables, and it does not include any NULL values.

Letโ€™s take another look at the example from the previous post:

A   |   B
-------------
a       c
b       d
e       e
f       f

The INNER JOIN will only extract the common values between A and B (e, f).

Result:

A   |   B
-------------
e       e
f       f

Basic INNER JOIN Example

Hereโ€™s a simple SQL query:

SELECT *
FROM computer
INNER JOIN software
ON computer.computer_id = software.computer_id;

This query retrieves only the rows where the software_id in the computer table matches the software_id in the software table.

๐Ÿ“Œ Example Result

  • INNER JOIN only shows data that exists in both tables, so there are no NULL values. For instance, computers like HP Spectre x360 and ASUS ZenBook with NULL values in software_id will not be included in the results.

  • However, LEFT JOIN uses the left table as the base, so NULL values can appear in the result.

  • With INNER JOIN, you will always get only the rows that exist in both tables.

INNER JOIN with Multiple Tables

INNER JOIN can also be applied to more than two tables.

For example, if we join computer, software, and operating_system, we can check the common data across all three tables:

SELECT *
FROM computer
INNER JOIN software
ON computer.software_id = software.software_id
INNER JOIN operating_system
ON software.os_id = operating_system.os_id;

๐Ÿ“Œ Example Result

  • The MacBook series matches with Xcode (software_id = 1) and macOS (os_id = 1)

  • The Lenovo Legion 5 matches with Microsoft Office (software_id = 3) and Windows (os_id = 2).

  • Only the rows that are connected across all three tables are included in the final result.

Visualization: Venn Diagram

INNER JOIN can be visualized as the intersection of sets.

  • For two tables: intersection area.

  • For three tables: the overlapping area shared by all three sets.

    ๐Ÿ‘‰ INNER JOIN = Only the data that exists in all tables.

  • Although INNER JOIN is often considered more efficient in terms of performance, in practice you may choose between INNER JOIN and LEFT JOIN depending on the use case.

Summary

  • INNER JOIN extracts only the intersection data.

  • It never includes NULL values.

  • You can join multiple tables, but the conditions get stricter, and the result set may shrink.

  • While INNER JOIN often has performance benefits compared to LEFT JOIN, you should choose the join type based on your requirements (INNER, LEFT, RIGHT, etc.).

๐Ÿ‘‰Check out the JOIN SQL queries example here

๐Ÿ‘‰Check out the spreadsheet JOIN example here

0
Subscribe to my newsletter

Read articles from Suin Lee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suin Lee
Suin Lee