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:

  • Departments table: Contains all currently active departments with their id and name
  • Students table: Contains student information including their department_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
StudentsID=1, Alice, Dept=999ID=2, Bob, Dept=1ID=3, Carol, Dept=777DepartmentsID=1, Computer SciID=2, MathematicsLEFT JOIN ProcessMatch on department_id = d.idJOIN Result1, Alice, 999, NULL (Invalid โŒ)2, Bob, 1, "Computer Sci" (Valid โœ…)3, Carol, 777, NULL (Invalid โŒ)WHERE d.id IS NULL โ†’ Returns Alice & Carol
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.0K Views
High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen