Lesson 7: Table Joins
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: