-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstud_with_invalid_dept.sql
38 lines (30 loc) · 1.01 KB
/
stud_with_invalid_dept.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*Table: Departments
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is 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 |
+---------------+---------+
id is 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.
Write an SQL query to find the id and the name of all students who are enrolled in departments that no longer exist.
*/
# Write your MySQL query statement below
SELECT s.id, s.name
FROM Students s LEFT JOIN Departments d
ON s.department_id=d.id
WHERE d.id IS NULL
#V2
SELECT id, name
FROM Students
WHERE department_id not in (SELECT id from Departments)