LEFT JOIN

LEFT JOIN: keep every row

SQLite in your browser
Lesson concept What you'll do
1 / 4

List every member or book, with their loans where they have any.

LEFT JOIN keeps every row of the first table, matched or not.

The database
8 rows
Click a name to type it
The database
9 rows
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How LEFT JOIN works

LEFT JOIN keeps every row of the first table (after FROM), matched or not.

members

member_idname
101Ava
102Ben
103Cara
104Dev

loans

loan_idmember_idloan_date
11012026-01-12
21032026-02-03
31012026-03-20
41022026-04-08
5105(deleted)2026-05-14
membersloans

query

SELECT members.name, loans.loan_date
FROM members LEFT JOIN loans
  ON members.member_id = loans.member_id;

result: 5 rows

nameloan_date
Ava2026-01-12
Ava2026-03-20
Ben2026-04-08
Cara2026-02-03
DevNULL

Keeps every member, matched or not. Dev has no loans, so loan_date is NULL.

Recap: every JOIN side by side
  • What does LEFT JOIN do?

    FROM members LEFT JOIN loans ON members.member_id = loans.member_id

    It keeps every row of the first table, whether or not it has a match. Where there's no match, the second table's columns are NULL.

  • Which table is "left"?

    The one written first, after FROM. Swap them and you keep every loan instead of every member.

  • What happens to a member with three loans?

    They appear three times, once per loan. A member with no loans appears once, with NULLs.

  • What about RIGHT JOIN?

    It keeps every row of the second table instead. Most people write LEFT JOIN and put the table they want to keep first.

  • How is this like Excel?

    It's a lookup that keeps every row of your list, showing #N/A where nothing was found, like Lookup & Merge's "keep all".

Other databases

How other databases do it

The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.

The older way to write LEFT JOIN
DatabaseHowNote
SQLite (these lessons)LEFT JOIN ... ONThe only way.
PostgreSQLLEFT JOIN ... ONThe only way.
MySQL / MariaDBLEFT JOIN ... ONThe only way.
SQL ServerLEFT JOIN ... ONOld code may have WHERE a.id *= b.id; it no longer runs.
OracleLEFT JOIN ... ONOld code writes WHERE m.member_id = l.member_id(+): the (+) goes on the side that may be empty.
Sybase ASELEFT JOIN ... ONOld code writes WHERE m.member_id *= l.member_id, which still runs.
RIGHT JOIN and FULL JOIN
DatabaseHowNote
SQLite (these lessons)BothSince SQLite 3.39 (2022).
PostgreSQLBoth
MySQL / MariaDBRIGHT onlyNo FULL JOIN: combine a LEFT and a RIGHT JOIN with UNION.
SQL ServerBoth
OracleBoth
Sybase ASERIGHT onlyNo FULL JOIN: combine a LEFT and a RIGHT JOIN with UNION.

Know someone fighting with a spreadsheet? Send them this page.