Academic Block

SQL Joins Get Certified

Learn how to combine related data from multiple tables using SQLite-compatible SQL JOIN queries.

What is a SQL JOIN?

A JOIN combines rows from two or more tables based on a related column. JOINs are one of the most important features of relational databases, because related information is usually stored in separate tables.

For example, a school database may keep student details in one table and exam results in another. A JOIN connects them through a common column such as student_id. Every example below creates its own small tables inside the query, so each one runs on its own.

Basic JOIN Syntax

The basic syntax names the two tables and uses ON to say which columns connect them.

What this code does: The players table stores only a team number, and the teams table stores the team names. JOIN ... ON players.team_id = teams.team_id pairs each player with the matching team, and the result has the columns player_name and team_name. The query returns 3 rows: Leo (Falcons), Maya (Rockets) and Noah (Falcons).

WITH players(player_id, player_name, team_id) AS (
  VALUES
    (1, 'Leo', 10),
    (2, 'Maya', 20),
    (3, 'Noah', 10)
),
teams(team_id, team_name) AS (
  VALUES
    (10, 'Falcons'),
    (20, 'Rockets')
)
SELECT
  players.player_name,
  teams.team_name
FROM players
JOIN teams
  ON players.team_id = teams.team_id;

INNER JOIN

An INNER JOIN returns only the rows that have a matching value in both tables. A plain JOIN means the same thing.

What this code does: Pairs each airport with its flights. Skyport has two flights, so it appears twice, and Harbor has one. Mountain Airport has no flights, so it has no match and is left out. The query returns 3 rows: Skyport Airport (Delhi, SK101), Skyport Airport (Delhi, SK303) and Harbor Airport (Mumbai, HB202).

WITH airports(airport_id, airport_name, city) AS (
  VALUES
    (1, 'Skyport Airport', 'Delhi'),
    (2, 'Harbor Airport', 'Mumbai'),
    (3, 'Mountain Airport', 'Leh')
),
flights(flight_id, airport_id, flight_code) AS (
  VALUES
    (101, 1, 'SK101'),
    (102, 2, 'HB202'),
    (103, 1, 'SK303')
)
SELECT
  a.airport_name,
  a.city,
  f.flight_code
FROM airports AS a
INNER JOIN flights AS f
  ON a.airport_id = f.airport_id;

INNER JOIN with WHERE

Combine an INNER JOIN with WHERE to filter the joined rows.

What this code does: First joins every dish to its chef, and then WHERE d.price > 400 keeps only the dishes that cost more than 400. Curry (380) and Soup (250) are removed. The query returns 2 rows: Anika (Pasta, 450) and Anika (Pizza, 550).

WITH chefs(chef_id, chef_name) AS (
  VALUES
    (1, 'Anika'),
    (2, 'Kabir'),
    (3, 'Rhea')
),
dishes(dish_id, chef_id, dish_name, price) AS (
  VALUES
    (101, 1, 'Pasta', 450),
    (102, 2, 'Curry', 380),
    (103, 1, 'Pizza', 550),
    (104, 3, 'Soup', 250)
)
SELECT
  c.chef_name,
  d.dish_name,
  d.price
FROM chefs AS c
INNER JOIN dishes AS d
  ON c.chef_id = d.chef_id
WHERE d.price > 400;

LEFT JOIN

A LEFT JOIN returns all rows from the left table plus the matching rows from the right table. When there is no match, the right-side columns are NULL.

What this code does: Every author is kept. Nora has two articles, Ivy has one, and Arman and Sam have none, so their title is NULL. The query returns 5 rows: Nora (The Solar System), Nora (Future Cities), Arman (NULL), Ivy (Ocean Life) and Sam (NULL).

WITH authors(author_id, author_name) AS (
  VALUES
    (1, 'Nora'),
    (2, 'Arman'),
    (3, 'Ivy'),
    (4, 'Sam')
),
articles(article_id, author_id, title) AS (
  VALUES
    (201, 1, 'The Solar System'),
    (202, 3, 'Ocean Life'),
    (203, 1, 'Future Cities')
)
SELECT
  a.author_name,
  ar.title
FROM authors AS a
LEFT JOIN articles AS ar
  ON a.author_id = ar.author_id
ORDER BY a.author_id;

LEFT JOIN with NULL

Add IS NULL to a LEFT JOIN to find rows that have no matching record in the other table.

What this code does: The LEFT JOIN keeps every workshop, and unmatched ones get NULL in the registration columns. WHERE r.registration_id IS NULL then keeps only the workshops nobody registered for. The query returns 2 rows: Robotics and Drawing.

WITH workshops(workshop_id, workshop_name) AS (
  VALUES
    (1, 'Photography'),
    (2, 'Robotics'),
    (3, 'Astronomy'),
    (4, 'Drawing')
),
registrations(registration_id, workshop_id, participant) AS (
  VALUES
    (101, 1, 'Ravi'),
    (102, 3, 'Meera'),
    (103, 1, 'Zoya')
)
SELECT
  w.workshop_name
FROM workshops AS w
LEFT JOIN registrations AS r
  ON w.workshop_id = r.workshop_id
WHERE r.registration_id IS NULL;

LEFT JOIN: Filtering in ON vs WHERE

A condition on the right table behaves differently in ON and in WHERE. In ON it limits which rows are matched, and the left rows are all kept.

What this code does: The title condition is part of ON, so all four authors are still returned, and only the matching titles are shown. If the same condition were moved to WHERE, the NULL rows would be filtered out and the LEFT JOIN would behave like an INNER JOIN, returning just 1 row: Nora (The Solar System). The query returns 4 rows: Nora (The Solar System), Arman (NULL), Ivy (NULL) and Sam (NULL).

WITH authors(author_id, author_name) AS (
  VALUES
    (1, 'Nora'),
    (2, 'Arman'),
    (3, 'Ivy'),
    (4, 'Sam')
),
articles(article_id, author_id, title) AS (
  VALUES
    (201, 1, 'The Solar System'),
    (202, 3, 'Ocean Life'),
    (203, 1, 'Future Cities')
)
SELECT
  a.author_name,
  ar.title
FROM authors AS a
LEFT JOIN articles AS ar
  ON a.author_id = ar.author_id
 AND ar.title LIKE 'The%'
ORDER BY a.author_id;

JOIN Using Table Aliases

Table aliases give tables short names and make JOIN queries easier to read.

What this code does: The aliases p and m stand for planets and moons. Mars appears twice because it has two moons, and Jupiter is left out because it has none in this table. 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;

JOIN with ORDER BY

After joining tables, ORDER BY arranges the resulting rows.

What this code does: Joins each score to its game and sorts the rows from the highest points to the lowest. Space Run appears twice because it has two scores. The query returns 4 rows: Space Run (1400), Castle Quest (1200), Ocean Race (950) and Space Run (850).

WITH games(game_id, game_name) AS (
  VALUES
    (1, 'Space Run'),
    (2, 'Castle Quest'),
    (3, 'Ocean Race')
),
scores(score_id, game_id, points) AS (
  VALUES
    (101, 1, 850),
    (102, 2, 1200),
    (103, 3, 950),
    (104, 1, 1400)
)
SELECT
  g.game_name,
  s.points
FROM games AS g
INNER JOIN scores AS s
  ON g.game_id = s.game_id
ORDER BY s.points DESC;

JOIN with GROUP BY

JOIN can be combined with GROUP BY and aggregate functions to summarize related records.

What this code does: Joins every sale to its store, adds up the amounts for each store with SUM(), and sorts the totals from the largest to the smallest. The query returns 3 rows: North Store (1300), Central Store (1250) and West Store (650).

WITH stores(store_id, store_name) AS (
  VALUES
    (1, 'Central Store'),
    (2, 'North Store'),
    (3, 'West Store')
),
sales(sale_id, store_id, amount) AS (
  VALUES
    (101, 1, 500),
    (102, 1, 750),
    (103, 2, 900),
    (104, 2, 400),
    (105, 3, 650)
)
SELECT
  st.store_name,
  SUM(s.amount) AS total_sales
FROM stores AS st
INNER JOIN sales AS s
  ON st.store_id = s.store_id
GROUP BY st.store_id, st.store_name
ORDER BY total_sales DESC;

JOIN Three Tables

You can join three or more tables when they are connected through related columns. Each extra table needs its own JOIN ... ON.

What this code does: Follows the chain customers → orders → products. The first join links a 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: Aarav (Tablet Stand), Aarav (Desk Light) and Meera (USB Hub).

WITH customers(customer_id, customer_name) AS (
  VALUES
    (1, 'Aarav'),
    (2, 'Meera'),
    (3, 'Tara')
),
orders(order_id, customer_id, product_id) AS (
  VALUES
    (101, 1, 10),
    (102, 2, 20),
    (103, 1, 30)
),
products(product_id, product_name) AS (
  VALUES
    (10, 'Tablet Stand'),
    (20, 'USB Hub'),
    (30, 'Desk Light')
)
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;

SELF JOIN

A SELF JOIN joins a table with itself. It is useful when rows in the same table are related to each other, and it needs two different aliases.

What this code does: The staff table is used twice: as e for the employee and as s for the supervisor. Each supervisor_id is matched to another row’s staff_id. Elena has no supervisor, so the LEFT JOIN keeps her with NULL. The result columns are employee and supervisor. The query returns 4 rows: Elena (NULL), Jon (Elena), Priya (Elena) and Samir (Jon).

WITH staff(staff_id, staff_name, supervisor_id) AS (
  VALUES
    (1, 'Elena', NULL),
    (2, 'Jon', 1),
    (3, 'Priya', 1),
    (4, 'Samir', 2)
)
SELECT
  e.staff_name AS employee,
  s.staff_name AS supervisor
FROM staff AS e
LEFT JOIN staff AS s
  ON e.supervisor_id = s.staff_id
ORDER BY e.staff_id;

CROSS JOIN

A CROSS JOIN returns every possible combination of rows from the two tables. It has no ON clause.

What this code does: Pairs each of the 3 materials with each of the 2 finishes, which gives 3 × 2 = 6 rows. The number of rows grows quickly with bigger tables. The query returns 6 rows: Glass (Gloss), Glass (Matte), Metal (Gloss), Metal (Matte), Wood (Gloss) and Wood (Matte).

WITH materials(material) AS (
  VALUES
    ('Wood'),
    ('Metal'),
    ('Glass')
),
finishes(finish) AS (
  VALUES
    ('Matte'),
    ('Gloss')
)
SELECT
  m.material,
  f.finish
FROM materials AS m
CROSS JOIN finishes AS f
ORDER BY m.material, f.finish;

RIGHT JOIN

A RIGHT JOIN keeps all rows from the right table. Modern SQLite (version 3.39 and later) supports it directly. In older versions, swap the table order and use LEFT JOIN.

What this code does: Every team is kept. Comets has no players, so its player column is NULL. The query returns 4 rows: Leo (Falcons), Noah (Falcons), Maya (Rockets) and NULL (Comets).

WITH players(player_id, player_name, team_id) AS (
  VALUES
    (1, 'Leo', 10),
    (2, 'Maya', 20),
    (3, 'Noah', 10)
),
teams(team_id, team_name) AS (
  VALUES
    (10, 'Falcons'),
    (20, 'Rockets'),
    (30, 'Comets')
)
SELECT
  p.player_name,
  t.team_name
FROM players AS p
RIGHT JOIN teams AS t
  ON p.team_id = t.team_id
ORDER BY t.team_id, p.player_id;

FULL OUTER JOIN

A FULL OUTER JOIN keeps the unmatched rows from both tables. SQLite supports it directly from version 3.39.

What this code does: Tripod and Microphone exist in both tables, so they are matched. Camera exists only on the left and Headphones only on the right, so each of those rows has NULL on the missing side. The query returns 4 rows: 1 (Camera, NULL), 2 (Tripod, Tripod), 3 (Microphone, Microphone) and 4 (NULL, Headphones).

WITH left_items(item_id, item_name) AS (
  VALUES
    (1, 'Camera'),
    (2, 'Tripod'),
    (3, 'Microphone')
),
right_items(item_id, item_name) AS (
  VALUES
    (2, 'Tripod'),
    (3, 'Microphone'),
    (4, 'Headphones')
)
SELECT
  COALESCE(l.item_id, r.item_id) AS item_id,
  l.item_name AS left_item,
  r.item_name AS right_item
FROM left_items AS l
FULL OUTER JOIN right_items AS r
  ON l.item_id = r.item_id
ORDER BY item_id;

FULL OUTER JOIN Alternative in SQLite

For older SQLite versions, a full outer join can be simulated with two LEFT JOIN queries combined with UNION ALL. The second query returns only the rows that did not match.

What this code does: The first query returns every left item with its match, and the second adds the right-side items that have no left match (Headphones). The two parts never overlap, so UNION ALL is enough, and the result is the same as the native FULL OUTER JOIN. The query returns 4 rows: 1 (Camera, NULL), 2 (Tripod, Tripod), 3 (Microphone, Microphone) and 4 (NULL, Headphones).

WITH left_items(item_id, item_name) AS (
  VALUES
    (1, 'Camera'),
    (2, 'Tripod'),
    (3, 'Microphone')
),
right_items(item_id, item_name) AS (
  VALUES
    (2, 'Tripod'),
    (3, 'Microphone'),
    (4, 'Headphones')
)
SELECT
  l.item_id AS item_id,
  l.item_name AS left_item,
  r.item_name AS right_item
FROM left_items AS l
LEFT JOIN right_items AS r
  ON l.item_id = r.item_id

UNION ALL

SELECT
  r.item_id AS item_id,
  l.item_name AS left_item,
  r.item_name AS right_item
FROM right_items AS r
LEFT JOIN left_items AS l
  ON r.item_id = l.item_id
WHERE l.item_id IS NULL
ORDER BY item_id;

JOIN with DISTINCT

The DISTINCT keyword removes duplicate rows from the join result.

What this code does: Lina has two concerts at City Hall, which would give two identical rows. DISTINCT collapses them into one. The query returns 3 rows: Lina (City Hall), Marco (River Arena) and Sia (City Hall).

WITH musicians(musician_id, musician_name) AS (
  VALUES
    (1, 'Lina'),
    (2, 'Marco'),
    (3, 'Sia')
),
concerts(concert_id, musician_id, venue) AS (
  VALUES
    (101, 1, 'City Hall'),
    (102, 1, 'City Hall'),
    (103, 2, 'River Arena'),
    (104, 3, 'City Hall')
)
SELECT DISTINCT
  m.musician_name,
  c.venue
FROM musicians AS m
INNER JOIN concerts AS c
  ON m.musician_id = c.musician_id
ORDER BY m.musician_name;

Common SQL JOIN Types

JOIN Type
Description
Result
INNER JOIN
Combines matching rows
Only matching records
LEFT JOIN
Keeps all rows from the left table
All left rows + matches
RIGHT JOIN
Keeps all rows from the right table
All right rows + matches
FULL OUTER JOIN
Keeps rows from both tables
Matches + unmatched rows
CROSS JOIN
Creates every combination
Cartesian product
SELF JOIN
Joins a table to itself
Related rows in one table

Advantages of SQL JOINs

  • Combine related information from different tables.
  • Reduce the need to duplicate data across tables.
  • Make relational database queries more powerful.
  • Work with filtering, sorting, grouping and aggregation.
  • Allow complex relationships between tables to be queried.

Best Practices

  • Always specify how the tables are related using ON.
  • Use table aliases to make long JOIN queries easier to read.
  • Use INNER JOIN when only matching records are required.
  • Use LEFT JOIN when all records from the main table are needed.
  • Put conditions on the right table in ON, not WHERE, when you want to keep unmatched left rows.
  • Select only the columns you need.
  • Be careful with CROSS JOIN, because it can produce a very large number of rows.

SQL JOINs connect related information stored in different tables. Understanding INNER, LEFT, RIGHT, FULL, CROSS and SELF joins is essential for working with relational databases. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.