LEFT JOIN

Updated · 9 min read
mediumPostgreSQL 18sqlleft-joinouter-joinanti-joinjoinsinterview

LEFT JOIN keeps every row of the left table, whether or not it finds a match, and fills the right table's columns with NULL when it doesn't. "How much has each student paid?" must include Advitha, who has paid nothing. An inner join would drop her, and a report missing a student is wrong in the quietest way possible. This lesson covers how LEFT JOIN works, finding rows that have no match, counting and totaling correctly, and the single most common outer join mistake: a WHERE condition that silently turns it back into an inner join.

In One Minute

  • FROM a LEFT JOIN b ON ... returns every row of a, matched with b where possible.
  • Unmatched rows get NULL in every column from b.
  • WHERE b.key IS NULL keeps only rows with no match: an anti-join.
  • Count matches with count(b.column), not count(*), and total them with coalesce(sum(...), 0).
  • A condition on b in WHERE removes the NULL rows; put it in ON to keep them.

The Basics

SQL
SELECT columns
FROM left_table
LEFT JOIN right_table ON left_table.key = right_table.key;

Every student with their payments:

SQL▶ Try it
SELECT s.first_name, p.payment_id, p.amount_usd
FROM students AS s
LEFT JOIN payments AS p ON p.student_id = s.student_id
ORDER BY s.student_id, p.payment_id;
Output
 first_name | payment_id | amount_usd
------------+------------+------------
 Srihitha   |          1 |     450.00
 Srihitha   |          2 |     250.00
 Srihitha   |          3 |     250.00
 Srihitha   |          4 |     550.00
 Srihitha   |          5 |     200.00
 Advitha    |       NULL |       NULL
 Navika     |          6 |     225.00
 Navika     |          7 |     225.00
(8 rows)

Srihitha and Navika appear once per payment, as with an inner join. Advitha appears once, with NULL for payment_id and amount_usd, meaning "no matching payment". LEFT OUTER JOIN is the same thing with an optional word. Which table is "left" matters: it's the one written first, after FROM, and it's the one whose rows are all kept.

Every student is kept; Advitha has no payments, so her payment columns are NULL

Finding Rows with No Match

Add WHERE right_table.key IS NULL to keep only the left rows that found nothing. Students who have never paid:

SQL▶ Try it
SELECT s.first_name
FROM students AS s
LEFT JOIN payments AS p ON p.student_id = s.student_id
WHERE p.payment_id IS NULL;
Output
 first_name
------------
 Advitha
(1 row)

This pattern is called an anti-join. Test a column that can't be NULL in a real match, such as the right table's primary key; then NULL can only mean "no match". The same pattern finds courses nobody has enrolled in (Git Basics), or enrollments with no payment yet:

SQL▶ Try it
SELECT e.student_id, e.course_id
FROM enrollments AS e
LEFT JOIN payments AS p
  ON p.student_id = e.student_id
 AND p.course_id  = e.course_id
WHERE p.payment_id IS NULL
ORDER BY e.student_id, e.course_id;
Output
 student_id | course_id
------------+-----------
        102 |         1
        102 |         4
(2 rows)

Advitha's two enrollments have no payments at all. EXISTS shows another way to write this, with NOT EXISTS.

LEFT JOIN keeps every student; adding WHERE ... IS NULL keeps only the students with no match

Counting and Totaling

With a left join, count a column from the right table, and wrap totals in coalesce. Payments per student:

SQL▶ Try it
SELECT s.first_name,
       count(p.payment_id) AS payments,
       coalesce(sum(p.amount_usd), 0.00) AS total_paid
FROM students AS s
LEFT JOIN payments AS p ON p.student_id = s.student_id
GROUP BY s.student_id, s.first_name
ORDER BY s.student_id;
Output
 first_name | payments | total_paid
------------+----------+------------
 Srihitha   |        5 |    1700.00
 Advitha    |        0 |       0.00
 Navika     |        2 |     450.00
(3 rows)

count(p.payment_id) counts only real payments, so Advitha gets 0. count(*) would count her one row of NULLs and report 1 payment, which is wrong. And sum over nothing is NULL, so coalesce turns her total into 0.00.

count(*) gives Advitha 1 for her NULL row; count(p.payment_id) correctly gives 0

The WHERE Trap

A condition on the right table in WHERE removes the NULL rows, turning the left join into an inner join. Every student with their December-onward payments, written two ways:

SQL▶ Try it
SELECT s.first_name, p.payment_id, p.amount_usd
FROM students AS s
LEFT JOIN payments AS p ON p.student_id = s.student_id
WHERE p.paid_on >= '2025-12-01'
ORDER BY s.student_id, p.payment_id;
Output
 first_name | payment_id | amount_usd
------------+------------+------------
 Srihitha   |          3 |     250.00
 Srihitha   |          4 |     550.00
 Srihitha   |          5 |     200.00
 Navika     |          7 |     225.00
(4 rows)

Advitha vanished. The join kept her with NULL in paid_on, then WHERE tested NULL >= '2025-12-01', which is unknown, so her row was dropped. Moving the condition into ON makes it part of the matching instead:

SQL▶ Try it
SELECT s.first_name, p.payment_id, p.amount_usd
FROM students AS s
LEFT JOIN payments AS p
  ON p.student_id = s.student_id
 AND p.paid_on >= '2025-12-01'
ORDER BY s.student_id, p.payment_id;
Output
 first_name | payment_id | amount_usd
------------+------------+------------
 Srihitha   |          3 |     250.00
 Srihitha   |          4 |     550.00
 Srihitha   |          5 |     200.00
 Advitha    |       NULL |       NULL
 Navika     |          7 |     225.00
(5 rows)

Now only December-onward payments count as matches, and every student is still kept, Advitha with NULLs. The rule: conditions that decide which right rows match go in ON; conditions that decide which result rows to keep go in WHERE.

In WHERE, the date test drops Advitha's NULL row; in ON, it only limits which payments match

A Real Report: What Each Enrollment Still Owes

Putting it together: every enrollment, its course fee, how much has been paid, and what's still owed. Payments may not exist, so they're left-joined:

SQL▶ Try it
SELECT e.student_id, c.title, c.fee_usd,
       coalesce(sum(p.amount_usd), 0.00) AS paid,
       c.fee_usd - coalesce(sum(p.amount_usd), 0.00) AS owed
FROM enrollments AS e
JOIN courses AS c ON c.course_id = e.course_id
LEFT JOIN payments AS p
  ON p.student_id = e.student_id
 AND p.course_id  = e.course_id
GROUP BY e.student_id, c.course_id, c.title, c.fee_usd
ORDER BY e.student_id, c.course_id;
Output
 student_id |       title       | fee_usd |  paid  |  owed
------------+-------------------+---------+--------+--------
        101 | Java Basics       |  450.00 | 450.00 |   0.00
        101 | Java OOP          |  500.00 | 500.00 |   0.00
        101 | Java Collections  |  550.00 | 550.00 |   0.00
        101 | SQL Basics        |  400.00 | 200.00 | 200.00
        102 | Java Basics       |  450.00 |   0.00 | 450.00
        102 | SQL Basics        |  400.00 |   0.00 | 400.00
        103 | JavaScript Basics |  450.00 | 450.00 |   0.00
(7 rows)

Srihitha owes $200.00 on SQL Basics, Advitha owes the full fee on both her courses, and Navika is paid up. Without the left join, Advitha's two rows would be missing, and the report would claim nobody owes anything for them.

Common Mistakes

Using an inner join where some rows have no match. Rows vanish without an error.

count(*) after a left join. Unmatched rows count as 1. Count a right-table column.

Filtering the right table in WHERE. It turns the join into an inner join. Use ON.

Testing a nullable column for the anti-join. WHERE p.method IS NULL also catches real payments with no method. Test the right table's primary key.

Interview Corner

What does a LEFT JOIN return?

Every row of the left table. Where a row has matches in the right table, one row per match; where it has none, one row with NULL in all the right table's columns.

How do you find rows in one table with no match in another?

LEFT JOIN the other table and keep WHERE other.primary_key IS NULL, an anti-join. NOT EXISTS is the other common way.

Why does adding WHERE right_table.column = value to a LEFT JOIN lose rows?

Unmatched rows have NULL in that column, and NULL = value is unknown, so WHERE drops them. Put the condition in ON to keep them.

Practice

Challenge: List every course with the number of students enrolled in it, including courses with none, sorted by course_id.

Show solution
SQL▶ Try it
SELECT c.title, count(e.student_id) AS students
FROM courses AS c
LEFT JOIN enrollments AS e ON e.course_id = c.course_id
GROUP BY c.course_id, c.title
ORDER BY c.course_id;
Output
       title       | students
-------------------+----------
 Java Basics       |        2
 Java OOP          |        1
 Java Collections  |        1
 SQL Basics        |        2
 JavaScript Basics |        1
 Git Basics        |        0
(6 rows)

Git Basics shows 0, because count(e.student_id) ignores its NULL row. With count(*), it would wrongly show 1.

Quick check: LEFT JOIN

3 questions
1students LEFT JOIN payments: how many rows does Advitha get?
2After students LEFT JOIN payments ... GROUP BY student, what does count(*) report for Advitha?
3Which query keeps every student while limiting matches to card payments?

Key Takeaways

You want toWrite
Every student, with paymentsFROM students s LEFT JOIN payments p ON p.student_id = s.student_id
Students with no payments... WHERE p.payment_id IS NULL
Count matchescount(p.payment_id)
Limit matches, keep all rowsLEFT JOIN payments p ON p.student_id = s.student_id AND p.paid_on >= '2025-12-01'

Was this tutorial helpful?

Lesson 37 of 115