SQL LEFT JOIN Get Certified
Learn how to keep every record from the left table while retrieving matching data from another table using the SQLite-compatible SQL LEFT JOIN statement.
What is LEFT JOIN?
The LEFT JOIN statement returns all rows from the left table. When a matching row exists in the right table, its columns are included. When there is no match, the right-side columns contain NULL.
LEFT JOIN is ideal when you need a complete list from one table (customers, categories, employees, rooms, etc.) and still want related information when it exists. Every example below builds its own small tables inside the query, so each one runs on its own.
Basic LEFT JOIN Syntax
The ON clause defines how the two tables are related. All left-table rows stay in the result.
What this code does: Lists every library. Central Library and River Library have members, so their member names appear. Hill Library has no members, so its member column is NULL. The query returns 3 rows: Central Library (Aarav), River Library (Meera) and Hill Library (NULL).
WITH libraries(library_id, library_name) AS (
VALUES
(1, 'Central Library'),
(2, 'River Library'),
(3, 'Hill Library')
),
memberships(member_id, library_id, member_name) AS (
VALUES
(101, 1, 'Aarav'),
(102, 2, 'Meera')
)
SELECT
l.library_name,
m.member_name
FROM libraries AS l
LEFT JOIN memberships AS m
ON l.library_id = m.library_id
ORDER BY l.library_id;
LEFT JOIN Keeps Unmatched Left Rows
Unlike INNER JOIN, LEFT JOIN never drops a left-table row simply because no match exists on the right.
What this code does: Shows every category even when some categories have no products. Cameras has two products, Audio has one, and both Lighting and Accessories appear with NULL product names. The query returns 5 rows.
WITH categories(category_id, category_name) AS (
VALUES
(1, 'Cameras'),
(2, 'Audio'),
(3, 'Lighting'),
(4, 'Accessories')
),
products(product_id, category_id, product_name) AS (
VALUES
(101, 1, 'Action Camera'),
(102, 1, 'Studio Camera'),
(103, 2, 'Wireless Speaker')
)
SELECT
c.category_name,
p.product_name
FROM categories AS c
LEFT JOIN products AS p
ON c.category_id = p.category_id
ORDER BY c.category_id;
Find Rows Without a Match (IS NULL)
A classic use of LEFT JOIN is to discover records that have no related row in another table. Filter with WHERE right_column IS NULL.
What this code does: Joins customers to orders and keeps only those customers who have never placed an order. Kabir and Mohit have no matching order rows, so they remain. The query returns 2 rows: Kabir and Mohit.
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Riya'),
(2, 'Kabir'),
(3, 'Tanya'),
(4, 'Mohit')
),
orders(order_id, customer_id, product) AS (
VALUES
(501, 1, 'Backpack'),
(502, 3, 'Headphones')
)
SELECT
c.customer_name
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL
ORDER BY c.customer_id;
LEFT JOIN with Table Aliases
Short table aliases keep longer LEFT JOIN queries readable.
What this code does: Shows every employee together with any assigned projects. Ishita has two projects and appears twice. Neha and Varun have no projects, so their project column is NULL. The query returns 5 rows.
WITH employees(employee_id, employee_name, department) AS (
VALUES
(1, 'Ishita', 'Design'),
(2, 'Rahul', 'Engineering'),
(3, 'Neha', 'Testing'),
(4, 'Varun', 'Support')
),
projects(project_id, employee_id, project_name) AS (
VALUES
(201, 1, 'Mobile Redesign'),
(202, 2, 'Smart Sensor'),
(203, 1, 'Website Refresh')
)
SELECT
e.employee_name,
e.department,
p.project_name
FROM employees AS e
LEFT JOIN projects AS p
ON e.employee_id = p.employee_id
ORDER BY e.employee_id;
LEFT JOIN with COUNT()
Combining LEFT JOIN with COUNT() lets you count related rows while still showing left-table records that have a count of zero.
What this code does: Counts workshops for every instructor. Nikhil has 2, Aditya has 1, and both Simran and Pooja have 0. The query returns 4 rows sorted by workshop count descending.
WITH instructors(instructor_id, instructor_name) AS (
VALUES
(1, 'Nikhil'),
(2, 'Simran'),
(3, 'Aditya'),
(4, 'Pooja')
),
workshops(workshop_id, instructor_id, workshop_name) AS (
VALUES
(101, 1, 'Photography Basics'),
(102, 1, 'Portrait Lighting'),
(103, 3, 'Digital Drawing')
)
SELECT
i.instructor_name,
COUNT(w.workshop_id) AS workshop_count
FROM instructors AS i
LEFT JOIN workshops AS w
ON i.instructor_id = w.instructor_id
GROUP BY i.instructor_id, i.instructor_name
ORDER BY workshop_count DESC, i.instructor_name;
LEFT JOIN with SUM() and COALESCE()
SUM() on a LEFT JOIN can produce NULL when a left row has no matching right rows. COALESCE() turns that NULL into a useful default (usually 0).
What this code does: Calculates total sales per region. North, South and East have sales; West has none, so COALESCE returns 0. The query returns 4 rows.
WITH sales_regions(region_id, region_name) AS (
VALUES
(1, 'North'),
(2, 'South'),
(3, 'East'),
(4, 'West')
),
sales(sale_id, region_id, amount) AS (
VALUES
(101, 1, 1200),
(102, 1, 800),
(103, 2, 1500),
(104, 3, 600)
)
SELECT
r.region_name,
COALESCE(SUM(s.amount), 0) AS total_sales
FROM sales_regions AS r
LEFT JOIN sales AS s
ON r.region_id = s.region_id
GROUP BY r.region_id, r.region_name
ORDER BY r.region_id;
LEFT JOIN with Multiple Conditions
Extra conditions can be placed in the ON clause. Only right-table rows that satisfy every condition are matched; unmatched left rows still appear with NULLs.
What this code does: Joins stores to inventory items that have quantity ≥ 10. Items with lower stock are treated as non-matches, so those stores still appear (with NULL item details when nothing qualifies). The query returns the matching high-stock items plus the stores that have none.
WITH stores(store_id, store_name) AS (
VALUES
(1, 'Downtown Store'),
(2, 'Airport Store'),
(3, 'Harbor Store'),
(4, 'Garden Store')
),
inventory(item_id, store_id, item_name, quantity) AS (
VALUES
(101, 1, 'Camera', 15),
(102, 1, 'Tripod', 4),
(103, 2, 'Headphones', 12),
(104, 2, 'Microphone', 3),
(105, 3, 'Speaker', 20)
)
SELECT
s.store_name,
i.item_name,
i.quantity
FROM stores AS s
LEFT JOIN inventory AS i
ON s.store_id = i.store_id
AND i.quantity >= 10
ORDER BY s.store_id;
LEFT JOIN with COALESCE for Defaults
When a left row has no match, the right-side columns are NULL. COALESCE replaces those NULLs with a friendly default value.
What this code does: Shows every player. Players with scores keep their points; players without any score receive 0 instead of NULL. The query returns 4 rows (Karan appears twice because he has two score rows).
WITH players(player_id, player_name) AS (
VALUES
(1, 'Karan'),
(2, 'Diya'),
(3, 'Manav'),
(4, 'Tia')
),
scores(score_id, player_id, points) AS (
VALUES
(101, 1, 75),
(102, 3, 92),
(103, 1, 88)
)
SELECT
p.player_name,
COALESCE(s.points, 0) AS points
FROM players AS p
LEFT JOIN scores AS s
ON p.player_id = s.player_id
ORDER BY p.player_id;
LEFT JOIN with Three Tables
Chain several LEFT JOINs when the data lives across more than two tables. Each additional table needs its own ON condition.
What this code does: Follows members → subscriptions → plans. Members without a subscription still appear (plan name is NULL). Aarohi has two plans and appears twice. The query returns 5 rows.
WITH members(member_id, member_name) AS (
VALUES
(1, 'Aarohi'),
(2, 'Yash'),
(3, 'Mira'),
(4, 'Rudra')
),
subscriptions(subscription_id, member_id, plan_id) AS (
VALUES
(201, 1, 10),
(202, 2, 20),
(203, 1, 30)
),
plans(plan_id, plan_name) AS (
VALUES
(10, 'Basic'),
(20, 'Premium'),
(30, 'Student')
)
SELECT
m.member_name,
p.plan_name
FROM members AS m
LEFT JOIN subscriptions AS s
ON m.member_id = s.member_id
LEFT JOIN plans AS p
ON s.plan_id = p.plan_id
ORDER BY m.member_id;
LEFT JOIN with CASE
A CASE expression can turn the presence or absence of a match into a clear status message.
What this code does: Labels each device. Devices that have a repair row show the repair status; devices with no repair show the text “No Repair”. The query returns 4 rows.
WITH devices(device_id, device_name) AS (
VALUES
(1, 'Printer'),
(2, 'Scanner'),
(3, 'Projector'),
(4, 'Router')
),
repairs(repair_id, device_id, repair_status) AS (
VALUES
(101, 1, 'Completed'),
(102, 3, 'Pending')
)
SELECT
d.device_name,
CASE
WHEN r.repair_id IS NULL THEN 'No Repair'
ELSE r.repair_status
END AS status
FROM devices AS d
LEFT JOIN repairs AS r
ON d.device_id = r.device_id
ORDER BY d.device_id;
LEFT JOIN with Dates
SQLite commonly stores dates as text in YYYY-MM-DD format. LEFT JOIN works the same way with date columns.
What this code does: Lists every room. Conference A and Training Room have bookings; Conference B and Meeting Room appear with NULL booking dates. The query returns 4 rows.
WITH rooms(room_id, room_name) AS (
VALUES
(1, 'Conference A'),
(2, 'Conference B'),
(3, 'Training Room'),
(4, 'Meeting Room')
),
bookings(booking_id, room_id, booking_date) AS (
VALUES
(101, 1, '2026-08-25'),
(102, 3, '2026-08-26')
)
SELECT
r.room_name,
b.booking_date
FROM rooms AS r
LEFT JOIN bookings AS b
ON r.room_id = b.room_id
ORDER BY r.room_id;
LEFT JOIN with LIMIT
LIMIT can restrict the number of rows returned after a LEFT JOIN.
What this code does: Joins stations to trains and returns only the first three stations in order. West Station is cut off by the LIMIT. The query returns 3 rows.
WITH stations(station_id, station_name) AS (
VALUES
(1, 'Central Station'),
(2, 'East Station'),
(3, 'North Station'),
(4, 'West Station')
),
trains(train_id, station_id, train_name) AS (
VALUES
(101, 1, 'Morning Express'),
(102, 2, 'City Runner'),
(103, 3, 'Night Express')
)
SELECT
s.station_name,
t.train_name
FROM stations AS s
LEFT JOIN trains AS t
ON s.station_id = t.station_id
ORDER BY s.station_id
LIMIT 3;
INNER JOIN vs LEFT JOIN
Common LEFT JOIN Uses
Advantages of LEFT JOIN
- Keeps every record from the left table.
- Includes matching records from the right table when they exist.
- Shows NULL when no matching right-side record exists.
- Ideal for finding missing or incomplete relationships.
- Works cleanly with COUNT(), SUM(), GROUP BY, ORDER BY, CASE and COALESCE.
- Can be chained across three or more tables.
Best Practices
- Use LEFT JOIN whenever every row from the left table must appear in the result.
- Place matching conditions in the
ONclause; useWHEREfor final filtering (especiallyIS NULL). - Use table aliases to keep multi-table queries readable.
- Use
COALESCE()when NULL values should become a meaningful default. - Remember that extra conditions in the
ONclause affect only which right rows match; left rows are still kept. - Create indexes on frequently joined columns for better performance on larger tables.
The LEFT JOIN statement preserves every row from the left table and brings in matching data from the right table when it exists. 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.