SQL JOIN clauses
A SQL JOIN clause merges multiple tables into a single result set. For these demos, we'll use the following schema:
INNER JOIN
An INNER JOIN matches column values across tables and only returns data where the specific match was found, typically by mapping a primary key to a foreign key.
SELECT
People.FirstName,
People.LastName,
EmailAddresses.EmailAddress,
FROM
People
INNER JOIN
EmailAddresses ON People.PersonId = EmailAddresses.PersonId
-- Sample output --
/*
FirstName | LastName | EmailAddress
---------------------------------------
Micky | Mouse | [email protected]
*/In this example the row for Micky Mouse in the People table was matched with a row from the EmailAddresses table through the PersonId key.
OUTER JOIN
An OUTER JOIN works similarly to an INNER JOIN except that it will include results where no match was found by returning null values for columns in the unmatched second table.
FULL OUTER JOIN
A FULL OUTER JOIN returns all rows from both tables (with nulls for unmatched results):
SELECT
People.FirstName,
People.LastName,
EmailAddresses.EmailAddress,
FROM
People
FULL OUTER JOIN
EmailAddresses ON People.PersonId = EmailAddresses.PersonId
-- Sample output --
/*
FirstName | LastName | EmailAddress
---------------------------------------
Micky | Mouse | [email protected]
Donald | Duck | null
null | null | [email protected]
*/In this example:
- The row for Micky Mouse in the
Peopletable was matched with a row from theEmailAddressestable through thePersonIdkey. - The row for Donald Duck in the
Peopletable was returned, but the email address wasnullbecause there was no match in theEmailAddressestable through thePersonIdkey - The row for the
mysteriousemail address in theEmailAddressestable was returned, but theFirstNameandLastNamewerenullbecause there was no match in thePeopletable through thePersonIdkey
LEFT OUTER JOIN
A LEFT OUTER JOIN will return all rows from the left table with null for non-matching right side table rows:
SELECT
People.FirstName,
People.LastName,
EmailAddresses.EmailAddress,
FROM
People
LEFT OUTER JOIN
EmailAddresses ON People.PersonId = EmailAddresses.PersonId
-- Sample output --
/*
FirstName | LastName | EmailAddress
---------------------------------------
Micky | Mouse | [email protected]
Donald | Duck | null
*/- The row for Micky Mouse in the
Peopletable was matched with a row from theEmailAddressestable through thePersonIdkey. - The row for Donald Duck in the
Peopletable was returned, but the email address wasnullbecause there was no match in theEmailAddressestable through thePersonIdkey
RIGHT OUTER JOIN
A RIGHT OUTER JOIN will return all rows from the right table with null for non-matching left side table rows:
SELECT
People.FirstName,
People.LastName,
EmailAddresses.EmailAddress,
FROM
People
LEFT OUTER JOIN
EmailAddresses ON People.PersonId = EmailAddresses.PersonId
-- Sample output --
/*
FirstName | LastName | EmailAddress
---------------------------------------
Micky | Mouse | [email protected]
null | null | [email protected]
*/In this example:
- The row for Micky Mouse in the
Peopletable was matched with a row from theEmailAddressestable through thePersonIdkey. - The row for the
mysteriousemail address in theEmailAddressestable was returned, but theFirstNameandLastNamewerenullbecause there was no match in thePeopletable through thePersonIdkey
Broader Topics Related to SQL JOIN clauses: INNER, OUTER, LEFT, RIGHT, FULL

Structured Query Language (SQL)
How to learn SQL: The language of relational data