← Back
LESSON 7
SQL (Core) - OUTER JOINs
Prereq: None | Progress: 0%
Course Syllabus
Lesson 0 📖 What is SQL? Lesson 1 📖 SELECT queries 101 Lesson 2 📖 Queries with constraints (Pt. 1) Lesson 3 📖 Queries with constraints (Pt. 2) Lesson 4 📖 Filtering & sorting results Review 1 📖 Simple SELECT Queries Lesson 6 📖 Multi-table queries with JOINs Lesson 7 📖 OUTER JOINs Lesson 8 📖 A short note on NULLs Lesson 9 📖 Queries with expressions Lesson 10 📖 Queries with aggregates (Pt. 1) Lesson 11 📖 Queries with aggregates (Pt. 2) Lesson 12 📖 Order of execution of a Query Lesson 13 📖 Inserting rows Lesson 14 📖 Updating rows Lesson 15 📖 Deleting rows Lesson 16 📖 Creating tables Lesson 17 📖 Altering tables Lesson 18 📖 Dropping tables Lesson 19 📖 Subqueries & Nested Queries Lesson 20 📖 Set Operations Summary 🎓 Course Completion
OUTER JOINs
While an INNER JOIN only retains rows that match in both tables, an OUTER JOIN preserves unmatched rows from one or both tables, populating missing values with NULL.
1. LEFT JOIN (LEFT OUTER JOIN) Syntax
SELECT building_name, role, name FROM buildings LEFT JOIN employees ON buildings.building_name = employees.building;
LEFT JOIN returns all records from the left table (buildings), regardless of whether a matching record exists in the right table (employees).
2. RIGHT JOIN & FULL OUTER JOIN
- RIGHT JOIN: Retains all rows from the right table, pairing with left rows when matching.
- FULL OUTER JOIN: Retains all rows from both tables, filling missing fields with
NULL.
💡
Unmatched Data Pattern
To find empty buildings with no assigned employees, use LEFT JOIN combined with WHERE employees.building IS NULL.
🗄️ Schema Explorer SELECTABLE
› buildings
|
🎯 Exercises Checklist:
☐ 1. Find the list of all buildings that have employees
☐ 2. Find the list of all buildings and their capacity
☐ 3. List all buildings and the distinct employee roles in each building (including empty buildings)
⚡ Practice Console
💡 Hint:
Loading hint...
🔑 Solution SQL:
SELECT * FROM movies; Data Output
No results. Run query to fetch data.