Students With Invalid Departments - Problem
๐ University Database Management Challenge
Imagine you're working as a database administrator for a university. The university has been restructuring, and some departments have been closed or merged. However, the student records still reference these non-existent departments!
Your task is to identify students who are enrolled in departments that no longer exist in the university's department registry. This is a classic data integrity issue that occurs frequently in real-world database management.
Given:
Departmentstable: Contains all currently active departments with theiridandnameStudentstable: Contains student information including theirdepartment_id(which may reference deleted departments)
Goal: Find all students whose department_id doesn't match any existing department id. Return their id and name.
This is a perfect example of finding orphaned records - a common database maintenance task!
Input & Output
example_1.sql โ Basic Case
$
Input:
Departments:
| id | name |
|----|----------------|
| 1 | Computer Science|
| 2 | Mathematics |
| 3 | Physics |
Students:
| id | name | department_id |
|----|-------|--------------|
| 1 | Alice | 7 |
| 2 | Bob | 1 |
| 3 | Carol | 8 |
โบ
Output:
| id | name |
|----|-------|
| 1 | Alice |
| 3 | Carol |
๐ก Note:
Alice is enrolled in department 7 and Carol in department 8, but only departments 1, 2, and 3 exist. Bob is in department 1 which exists, so he's not included in the result.
example_2.sql โ All Students Valid
$
Input:
Departments:
| id | name |
|----|--------|
| 1 | Biology |
| 2 | Chemistry|
Students:
| id | name | department_id |
|----|------|--------------|
| 1 | John | 1 |
| 2 | Jane | 2 |
| 3 | Mike | 1 |
โบ
Output:
| id | name |
|----|----- |
(empty result)
๐ก Note:
All students are enrolled in valid departments (1 or 2), so no students have invalid department references.
example_3.sql โ Edge Case with NULL
$
Input:
Departments:
| id | name |
|----|------|
| 1 | Art |
Students:
| id | name | department_id |
|----|-------|--------------|
| 1 | Sarah | NULL |
| 2 | Tom | 5 |
| 3 | Lisa | 1 |
โบ
Output:
| id | name |
|----|------|
| 2 | Tom |
๐ก Note:
Tom is in department 5 which doesn't exist. Sarah has NULL department_id (typically handled separately in real systems). Lisa is in valid department 1.
Constraints
- 1 โค Students.id, Departments.id โค 105
- 1 โค Students.name.length, Departments.name.length โค 20
- Students.department_id can reference non-existent departments
- All department IDs in Departments table are unique
- All student IDs in Students table are unique
Visualization
Tap to expand
Understanding the Visualization
1
Prepare Tables
Database engine loads Students and Departments tables into memory
2
LEFT JOIN Execution
For each student, attempt to match department_id with Departments.id
3
Preserve All Students
Keep all student records, set department columns to NULL when no match found
4
Filter Results
Apply WHERE d.id IS NULL to find students with no matching department
5
Return Results
Output the ID and name of students with invalid departments
Key Takeaway
๐ฏ Key Insight: LEFT JOIN is the perfect tool for finding orphaned records - it preserves all records from the left table while showing NULL for missing matches on the right.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code