Lesson 7: Table Joins

35 min read Intermediate Lesson 7 of 12 12.4k learners

What are Joins?

In relational databases, data is often spread across multiple tables. A JOIN combines rows from two or more tables based on a related column.

INNER JOIN

Returns rows that have matching values in both tables.

SQL
SELECT c.first_name, c.last_name, o.order_id
FROM customers c
INNER JOIN orders o
    ON c.customer_id = o.customer_id;

LEFT JOIN

Returns all rows from the left table, and matching rows from the right table. Unmatched rows show NULL values.

SQL
SELECT c.first_name, c.last_name, o.order_id
FROM customers c
LEFT JOIN orders o
    ON c.customer_id = o.customer_id;

RIGHT JOIN

Returns all rows from the right table, and matching rows from the left table.

SQL
SELECT c.first_name, c.last_name, o.order_id
FROM customers c
RIGHT JOIN orders o
    ON c.customer_id = o.customer_id;

FULL JOIN

Returns all rows from both tables, matching when possible, and filling NULLs when there’s no match. (Note: not supported in MySQL, but available in PostgreSQL, SQL Server, Oracle).

SQL
SELECT c.first_name, c.last_name, o.order_id
FROM customers c
FULL JOIN orders o
    ON c.customer_id = o.customer_id;

Practice Quiz

Test your understanding of SQL joins:

1. Which join returns only matching rows?
2. Which join includes all rows from the left table?
3. Which join is not supported in MySQL?
4. Which join returns all rows from both tables?
5. Which join ensures no data loss from the right table?