Chapter 33: PostgreSQL LEFT JOIN

PostgreSQL LEFT JOIN β˜•πŸ›‘οΈ

Last time we covered INNER JOIN β€” which only shows matching rows from both tables. Today we go to LEFT JOIN β€” which says:

β€œI want every single row from the left table, no matter what. If there is a matching row in the right table β†’ great, show it. If there is no match β†’ still show the left row, but put NULL in the right table’s columns.”

This is extremely common because in real life you almost always want to see all records from your main entity (customers, students, products, orders…) even if some of them don’t have related data yet.

1. What LEFT JOIN really does (teacher drawing on imaginary board)

Imagine two circles:

  • Left circle = students (the main table you care about)
  • Right circle = enrollments (optional related data)

INNER JOIN β†’ only the overlap (students who enrolled) LEFT JOIN β†’ entire left circle + overlap β†’ Students with no enrollments still appear β†’ with NULLs in enrollment columns

Official PostgreSQL wording (from docs):

LEFT OUTER JOIN returns all rows from the left table and the matched rows from the right table. The result is NULL from the right side if there is no match.

(Note: people usually just say LEFT JOIN β€” the word OUTER is optional and almost never written)

2. Basic syntax (the shape you will type 1,000 times)

SQL
  • The table written first (after FROM) is the left table
  • Everything from left table is kept
  • Matching rows from right table are added
  • No match β†’ right columns = NULL

3. Real, detailed example – using our college schema

Tables (same as last time):

students (left table – we want all students)

id first_name last_name email gpa
1 Aarav Patel aarav@hyd.edu 8.95
2 Priya Reddy priya@gmail.com 9.60
3 Rahul Sharma rahul@yahoo.com 7.85
4 Sneha Kumar sneha@gmail.com 9.20
5 Vikram Naik vikram@hyd.edu 8.40

enrollments (right table – optional)

id student_id course_id grade enrolled_at
1 1 1 A 2025-08-01
2 1 2 A+ 2025-08-02
3 2 1 B+ 2025-08-01
4 4 3 A 2025-08-05
5 5 2 B 2025-08-10

(Rahul id=3 has no enrollment)

Example 1: Basic LEFT JOIN – all students + their enrollments

SQL

Result:

text

β†’ Rahul appears! β†’ but with NULLs in enrollment columns β†’ This is the classic use-case of LEFT JOIN

Example 2: LEFT JOIN + multiple tables + COALESCE (very real pattern)

SQL

Result (partial):

text

β†’ COALESCE turns ugly NULLs into nice readable text

Example 3: Find students with no enrollments (very common report)

SQL

Result:

text

β†’ This is the classic way to find β€œorphaned” / β€œnon-related” records β†’ Very important interview question pattern

4. LEFT JOIN vs INNER JOIN – quick comparison (same data)

Query type Rows returned Rahul included? Use case feeling
INNER JOIN 5 rows No β€œOnly enrolled students”
LEFT JOIN 6 rows Yes (with NULLs) β€œAll students β€” show enrollment if exists”

5. Best practices & common mistakes (2026 production tips)

Mistake / Question Problem / Surprise Fix / Recommendation
Forget WHERE … IS NULL No rows for non-matches Use WHERE right.id IS NULL for non-matching rows
Use LEFT when you really want INNER Too many rows with NULLs Choose join type based on business question
LEFT JOIN on wrong column All rows appear with NULLs Double-check ON condition (fk = pk)
Performance on large tables Slow if no index on join columns Index foreign key columns (almost always)
LEFT JOIN + WHERE on right table column Turns into INNER JOIN! Move right-table conditions to ON clause or use COALESCE

Dangerous pattern (very common mistake):

SQL

β†’ Becomes equivalent to INNER JOIN β†’ Fix: move to ON … AND e.grade = ‘A’ or use COALESCE

Your mini homework right now

  1. Write a LEFT JOIN query showing all students + their grades (use COALESCE for ‘Not enrolled’)
  2. Modify it to show only students with no enrollments (WHERE … IS NULL)
  3. Add a third LEFT JOIN to also show course names
  4. Try the dangerous mistake above β†’ see how Rahul disappears

Next class?

Tell me:

  • Want to compare LEFT JOIN vs RIGHT JOIN vs FULL OUTER JOIN with same data?
  • LEFT JOIN with multiple tables (3–4 way join)?
  • LEFT JOIN + aggregate (COUNT, SUM, AVG) – e.g. students + number of courses?
  • Performance differences INNER vs LEFT + index usage?

Your guru is ready β€” what’s the next topic? πŸš€

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *