SQL INNER JOIN Get Certified
Learn how to combine matching records from two or more tables using the SQLite-compatible SQL INNER JOIN statement.
What is INNER JOIN?
The INNER JOIN statement combines rows from two tables when there is a matching value in both of them. Only the records that satisfy the join condition appear in the result, and rows with no match are dropped.
INNER JOIN is used when related information is stored in separate tables, for example customer details in one table and their purchases in another. Every example below builds its own small tables inside the query, so each one runs on its own.
Basic INNER JOIN Syntax
The ON clause says which columns connect the two tables. Only the rows that satisfy this condition are returned.
What this code does: Pairs each artist with their albums by matching artists.artist_id to albums.artist_id. Ava has two albums, so she appears twice. Mira has no albums, so she has no match and is missing from the result. The result has the columns artist_name and album_name. The query returns 3 rows: Ava (First Light), Ava (Open Roads) and Leo (Night Waves).
WITH artists(artist_id, artist_name) AS (
VALUES
(1, 'Ava'),
(2, 'Leo'),
(3, 'Mira')
),
albums(album_id, artist_id, album_name) AS (
VALUES
(101, 1, 'First Light'),
(102, 2, 'Night Waves'),
(103, 1, 'Open Roads')
)
SELECT
artists.artist_name,
albums.album_name
FROM artists
INNER JOIN albums
ON artists.artist_id = albums.artist_id;
JOIN Means INNER JOIN
In SQL, a plain JOIN is shorthand for INNER JOIN. Both forms give the same result.
What this code does: Leaves out the word INNER, and the query still returns exactly the same rows as the previous example. Many developers write INNER JOIN anyway, because it states the intention clearly. The query returns 3 rows: Ava (First Light), Ava (Open Roads) and Leo (Night Waves).
WITH artists(artist_id, artist_name) AS (
VALUES
(1, 'Ava'),
(2, 'Leo'),
(3, 'Mira')
),
albums(album_id, artist_id, album_name) AS (
VALUES
(101, 1, 'First Light'),
(102, 2, 'Night Waves'),
(103, 1, 'Open Roads')
)
SELECT
artists.artist_name,
albums.album_name
FROM artists
JOIN albums
ON artists.artist_id = albums.artist_id;
INNER JOIN with USING
When both tables use the same column name for the connection, USING (column) is a shorter alternative to ON.
What this code does: Both tables have an artist_id column, so USING (artist_id) joins on it. A USING column does not need a table prefix in the query. The result is the same as the ON version. The query returns 3 rows: Ava (First Light), Ava (Open Roads) and Leo (Night Waves).
WITH artists(artist_id, artist_name) AS (
VALUES
(1, 'Ava'),
(2, 'Leo'),
(3, 'Mira')
),
albums(album_id, artist_id, album_name) AS (
VALUES
(101, 1, 'First Light'),
(102, 2, 'Night Waves'),
(103, 1, 'Open Roads')
)
SELECT
artist_name,
album_name
FROM artists
INNER JOIN albums
USING (artist_id);
INNER JOIN with Table Aliases
Table aliases make INNER JOIN queries shorter and easier to read.
What this code does: The aliases p and m stand for planets and moons. Mars has two moons and appears twice, while Jupiter has no moons in this table and is left out. The query returns 3 rows: Earth (Moon), Mars (Phobos) and Mars (Deimos).
WITH planets(planet_id, planet_name) AS (
VALUES
(1, 'Earth'),
(2, 'Mars'),
(3, 'Jupiter')
),
moons(moon_id, planet_id, moon_name) AS (
VALUES
(101, 1, 'Moon'),
(102, 2, 'Phobos'),
(103, 2, 'Deimos')
)
SELECT
p.planet_name,
m.moon_name
FROM planets AS p
INNER JOIN moons AS m
ON p.planet_id = m.planet_id;
INNER JOIN with WHERE
Use a WHERE clause after an INNER JOIN to filter the matching records.
What this code does: Joins every drink to its cafe, and then keeps only the drinks that cost more than 150. Green Tea (120) and Lemon Tea (100) are removed. The query returns 2 rows: Bean House (Cold Coffee, 180) and Bean House (Mocha, 220).
WITH cafes(cafe_id, cafe_name) AS (
VALUES
(1, 'Bean House'),
(2, 'Morning Cup'),
(3, 'Green Cafe')
),
drinks(drink_id, cafe_id, drink_name, price) AS (
VALUES
(101, 1, 'Cold Coffee', 180),
(102, 2, 'Green Tea', 120),
(103, 1, 'Mocha', 220),
(104, 3, 'Lemon Tea', 100)
)
SELECT
c.cafe_name,
d.drink_name,
d.price
FROM cafes AS c
INNER JOIN drinks AS d
ON c.cafe_id = d.cafe_id
WHERE d.price > 150;
INNER JOIN with ORDER BY
ORDER BY sorts the rows returned by an INNER JOIN.
What this code does: Joins each exhibit to its museum and sorts the rows by visitor count, from the highest to the lowest. The query returns 4 rows: Science Hall (Robot Zone, 1100), Science Hall (Space Lab, 850), Art Gallery (Modern Art, 740) and History Center (Ancient Coins, 620).
WITH museums(museum_id, museum_name) AS (
VALUES
(1, 'Science Hall'),
(2, 'History Center'),
(3, 'Art Gallery')
),
exhibits(exhibit_id, museum_id, exhibit_name, visitors) AS (
VALUES
(101, 1, 'Space Lab', 850),
(102, 2, 'Ancient Coins', 620),
(103, 1, 'Robot Zone', 1100),
(104, 3, 'Modern Art', 740)
)
SELECT
m.museum_name,
e.exhibit_name,
e.visitors
FROM museums AS m
INNER JOIN exhibits AS e
ON m.museum_id = e.museum_id
ORDER BY e.visitors DESC;
INNER JOIN with Multiple Conditions
An INNER JOIN can use more than one condition in ON, combined with AND. A row must satisfy all of them.
What this code does: A shipment matches a warehouse only if the warehouse ID and the city are equal. Shipment 103 has the right warehouse (North Depot) but its city is Mumbai, not Delhi, so it is not matched. The query returns 3 rows: North Depot (Monitors), West Depot (Keyboards) and East Depot (Cables).
WITH warehouses(warehouse_id, warehouse_name, city) AS (
VALUES
(1, 'North Depot', 'Delhi'),
(2, 'West Depot', 'Pune'),
(3, 'East Depot', 'Kolkata')
),
shipments(shipment_id, warehouse_id, city, item) AS (
VALUES
(101, 1, 'Delhi', 'Monitors'),
(102, 2, 'Pune', 'Keyboards'),
(103, 1, 'Mumbai', 'Printers'),
(104, 3, 'Kolkata', 'Cables')
)
SELECT
w.warehouse_name,
s.item
FROM warehouses AS w
INNER JOIN shipments AS s
ON w.warehouse_id = s.warehouse_id
AND w.city = s.city;
INNER JOIN with DISTINCT
DISTINCT removes duplicate rows from the result of an INNER JOIN.
What this code does: Nina has two Physics classes, which would produce two identical rows. DISTINCT collapses them into one. The query returns 3 rows: Nina (Physics), Omar (Mathematics) and Sara (Physics).
WITH teachers(teacher_id, teacher_name) AS (
VALUES
(1, 'Nina'),
(2, 'Omar'),
(3, 'Sara')
),
classes(class_id, teacher_id, subject) AS (
VALUES
(101, 1, 'Physics'),
(102, 1, 'Physics'),
(103, 2, 'Mathematics'),
(104, 3, 'Physics')
)
SELECT DISTINCT
t.teacher_name,
c.subject
FROM teachers AS t
INNER JOIN classes AS c
ON t.teacher_id = c.teacher_id;
INNER JOIN with Aggregate Functions
INNER JOIN can be combined with COUNT(), SUM() and other aggregate functions to summarize related data.
What this code does: Joins each worker to a department, groups the rows by department, and calculates the number of workers and the total salary for each. The result has the columns department_name, employee_count and total_salary. Departments are sorted by total salary, from highest to lowest. The query returns 3 rows: Engineering (2, 137000), Design (2, 90000) and Support (1, 39000).
WITH departments(department_id, department_name) AS (
VALUES
(1, 'Design'),
(2, 'Engineering'),
(3, 'Support')
),
workers(worker_id, department_id, salary) AS (
VALUES
(101, 1, 42000),
(102, 1, 48000),
(103, 2, 65000),
(104, 2, 72000),
(105, 3, 39000)
)
SELECT
d.department_name,
COUNT(w.worker_id) AS employee_count,
SUM(w.salary) AS total_salary
FROM departments AS d
INNER JOIN workers AS w
ON d.department_id = w.department_id
GROUP BY d.department_id, d.department_name
ORDER BY total_salary DESC;
INNER JOIN with Three Tables
Use several INNER JOIN clauses to combine three or more related tables. Each extra table needs its own ON condition.
What this code does: Follows the chain customers → orders → products. The first join links each customer to their orders, and the second links each order to a product name. Tara has no orders, so she does not appear. The query returns 3 rows: Arjun (Travel Bag), Riya (Smart Watch) and Riya (Desk Organizer).
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Riya'),
(2, 'Arjun'),
(3, 'Tara')
),
orders(order_id, customer_id, product_id) AS (
VALUES
(501, 1, 10),
(502, 2, 20),
(503, 1, 30)
),
products(product_id, product_name) AS (
VALUES
(10, 'Smart Watch'),
(20, 'Travel Bag'),
(30, 'Desk Organizer')
)
SELECT
c.customer_name,
p.product_name
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN products AS p
ON o.product_id = p.product_id
ORDER BY c.customer_name;
INNER JOIN with Calculations
You can calculate with columns that come from the joined tables.
What this code does: Multiplies the quantity and the price from the sales table and labels the result total, while taking the item name from the items table. Rows are sorted by total, highest first. The query returns 3 rows: Folder (3, 120, 360), Notebook (4, 80, 320) and Marker (6, 40, 240).
WITH items(item_id, item_name) AS (
VALUES
(1, 'Notebook'),
(2, 'Marker'),
(3, 'Folder')
),
sales(sale_id, item_id, quantity, price) AS (
VALUES
(101, 1, 4, 80),
(102, 2, 6, 40),
(103, 3, 3, 120)
)
SELECT
i.item_name,
s.quantity,
s.price,
s.quantity * s.price AS total
FROM items AS i
INNER JOIN sales AS s
ON i.item_id = s.item_id
ORDER BY total DESC;
INNER JOIN with BETWEEN
Joined results can be filtered with other operators such as BETWEEN.
What this code does: Keeps the athletes whose score is from 80 to 100. Dev scored 74, which is outside the range. The query returns 2 rows: Kian (85) and Asha (92).
WITH athletes(athlete_id, athlete_name) AS (
VALUES
(1, 'Kian'),
(2, 'Asha'),
(3, 'Dev')
),
performances(performance_id, athlete_id, score) AS (
VALUES
(101, 1, 85),
(102, 2, 92),
(103, 3, 74)
)
SELECT
a.athlete_name,
p.score
FROM athletes AS a
INNER JOIN performances AS p
ON a.athlete_id = p.athlete_id
WHERE p.score BETWEEN 80 AND 100;
INNER JOIN with IN
IN filters the joined rows against several possible values.
What this code does: Keeps only the trips to Delhi or Pune. The Comet appears once here, because its other trip goes to Jaipur. The query returns 2 rows: Comet (Delhi) and Voyager (Pune).
WITH vehicles(vehicle_id, model) AS (
VALUES
(1, 'Comet'),
(2, 'Falcon'),
(3, 'Voyager')
),
trips(trip_id, vehicle_id, destination) AS (
VALUES
(101, 1, 'Delhi'),
(102, 2, 'Mumbai'),
(103, 3, 'Pune'),
(104, 1, 'Jaipur')
)
SELECT
v.model,
t.destination
FROM vehicles AS v
INNER JOIN trips AS t
ON v.vehicle_id = t.vehicle_id
WHERE t.destination IN ('Delhi', 'Pune');
INNER JOIN vs LEFT JOIN in Practice
The main difference is how unmatched rows are handled. INNER JOIN removes them, while LEFT JOIN keeps every row from the left table.
What this code does: This is the LEFT JOIN version of the very first example. Mira has no albums, so the INNER JOIN left her out, but here she is kept with a NULL album. The query returns 4 rows: Ava (First Light), Ava (Open Roads), Leo (Night Waves) and Mira (NULL).
WITH artists(artist_id, artist_name) AS (
VALUES
(1, 'Ava'),
(2, 'Leo'),
(3, 'Mira')
),
albums(album_id, artist_id, album_name) AS (
VALUES
(101, 1, 'First Light'),
(102, 2, 'Night Waves'),
(103, 1, 'Open Roads')
)
SELECT
artists.artist_name,
albums.album_name
FROM artists
LEFT JOIN albums
ON artists.artist_id = albums.artist_id;
INNER JOIN vs LEFT JOIN
Advantages of INNER JOIN
- Returns only related records from the joined tables.
- Useful for querying relationships between tables.
- Works with
WHERE,GROUP BY,ORDER BYand aggregate functions. - Can combine several tables in one query.
- Helps retrieve precise and relevant information.
Best Practices
- Always specify how the tables are related using
ON(orUSING). - Use table aliases when a query contains several tables.
- Select only the columns you need.
- Use meaningful column names or aliases in the result.
- Make sure the joined columns contain compatible values.
- Remember that unmatched rows disappear. Use
LEFT JOINif you need to keep them. - Create indexes on frequently joined columns to improve performance on larger databases.
The INNER JOIN statement combines related records from multiple tables and returns only the rows that satisfy the matching condition. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.