← Lesson
BitWithBite
SQL · Quick Reference

Lesson 8 — JOINs: Combining Multiple Tables Cheat Sheet

SQL
In one line: Without JOINs, you'd repeat every student's name and age in every enrollment row. That's data redundancy — it wastes space and creates inconsistency when data changes.

Key Ideas

1Why JOINs Exist. Without JOINs, you'd repeat every student's name and age in every enrollment row. That's data redundancy — it wastes space and creates inconsistency when data changes.

Code Examples

-- Three related tables CREATE TABLE courses ( id INT PRIMARY KEY AUTO_INCREMENT, course_name VARCHAR(100) NOT NULL, teacher VARCHAR(100) ); CREATE TABLE enrollments ( id INT PRIMARY KEY AUTO_INCREMENT, student_id ...
-- INNER JOIN: only rows that match in BOTH tables SELECT s.name, c.course_name, e.enrolled_on FROM students s INNER JOIN enrollments e ON s.id = e.student_id INNER JOIN courses c ON e.course_id = c.id; -- Shorter: JOIN = INNER JOIN SELECT s.nam...
-- LEFT JOIN: ALL rows from left table + matching rows from right -- Non-matching rows in right table get NULL SELECT s.name, c.course_name FROM students s LEFT JOIN enrollments e ON s.id = e.student_id LEFT JOIN courses c ON e.course_id = c.id; -...