Description
Table: Departments
| Column Name | Type |
|---|---|
| id | int |
| name | varchar |
- In SQL,
idis the primary key of this table. - The table has information about the id of each department of a university.
Table: Students
| Column Name | Type |
|---|---|
| id | int |
| name | varchar |
| department_id | int |
- In SQL,
idis the primary key of this table. - The table has information about the id of each student at a university and the id of the department he/she studies at.
Problem Statement
Find the id and the name of all students who are enrolled in departments that no longer exist.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Departmentstable:
| id | name |
|---|---|
| 1 | Electrical Engineering |
| 7 | Computer Engineering |
| 13 | Bussiness Administration |
Studentstable:
| 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 |
| 11 | Madelynn | 1 |
Output:
| id | name |
|---|---|
| 2 | John |
| 7 | Daiana |
| 4 | Jasmine |
| 3 | Steve |
Explanation:
- John, Daiana, Steve, and Jasmine are enrolled in departments 14, 33, 74, and 77 respectively. department 14, 33, 74, and 77 do not exist in the Departments table.
Solution
You do need to join the two tables based on deparment_id. You have to join as outer join and can help fetch the students who are enrolled in non-existing department.
1SELECT s.id, s.name
2 FROM Students s
3 LEFT OUTER JOIN Departments d
4 ON s.department_id = d.id
5 WHERE d.id IS NULL;


Comments