Given two tables Departments and Students, find all students who are enrolled in departments that no longer exist.
The Departments table contains information about university departments with columns:
id- Primary key identifying each departmentname- Name of the department
The Students table contains information about students with columns:
id- Primary key identifying each studentname- Name of the studentdepartment_id- Foreign key referencing the department
Return the id and name of all students whose department_id does not exist in the Departments table. The result can be returned in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key identifying each department |
name
|
varchar | Name of the department |
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key identifying each student |
name
|
varchar | Name of the student |
department_id
|
int | Foreign key referencing the department |
Input & Output
| id | name |
|---|---|
| 1 | Electrical Engineering |
| 7 | Computer Engineering |
| 13 | Business Administration |
| id | name | department_id |
|---|---|---|
| 23 | Alice | 1 |
| 1 | Bob | 7 |
| 5 | Jennifer | 13 |
| 2 | John | 14 |
| 4 | Jasmine | 77 |
| 3 | Steve | 74 |
| 6 | Luis | 1 |
| 8 | Jonathan | 7 |
| 7 | Daiana | 33 |
| id | name |
|---|---|
| 2 | John |
| 4 | Jasmine |
| 3 | Steve |
| 7 | Daiana |
Students John (id=2), Jasmine (id=4), Steve (id=3), and Daiana (id=7) are enrolled in departments with IDs 14, 77, 74, and 33 respectively. These department IDs do not exist in the Departments table, so these students are enrolled in invalid departments.
| id | name |
|---|---|
| 1 | Mathematics |
| 2 | Physics |
| id | name | department_id |
|---|---|---|
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| id | name |
|---|
All students are enrolled in valid departments (Alice in Mathematics, Bob in Physics), so no students have invalid departments. The result is empty.
| id | name |
|---|
| id | name | department_id |
|---|---|---|
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Since no departments exist in the Departments table, all students are enrolled in invalid departments and should be returned in the result.
Constraints
-
1 ≤ Departments.id ≤ 1000 -
1 ≤ Students.id ≤ 1000 -
1 ≤ Students.department_id ≤ 1000 -
Departments.nameandStudents.nameconsist of English letters and spaces