Eroxl's Notes
JOIN Clause (DML)

The JOIN clause combines rows from multiple tables based on related columns.

Basic Syntax

SELECT student.name, course.title
FROM enrollment
INNER JOIN student ON enrollment.student_id = student.student_id
INNER JOIN course ON enrollment.course_id = course.course_id;

Join three tables together to get student names and their enrolled courses.

Types of Joins

Inner Join

INNER JOIN returns only matching rows from both tables.

SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;

Returns only orders that have a matching customer.

Left Join

LEFT JOIN returns all rows from the left table and matching rows from the right.

SELECT customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Returns all customers, including those without orders.

Right Join

RIGHT JOIN returns all rows from the right table and matching rows from the left.

SELECT customers.customer_name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

Returns all orders, including those without a matching customer.

Full Outer Join

FULL OUTER JOIN returns all rows when there's a match in either table.

SELECT customers.customer_name, orders.order_id
FROM customers
FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;

Returns all customers and all orders, matching them where possible.