Skip to content

Commit 422ad4c

Browse files
add 1350
1 parent e51b36e commit 422ad4c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ _If you like this project, please leave me a star._ ★
882882

883883
| # | Title | Solutions | Video | Difficulty | Tag
884884
|-----|----------------|---------------|---------------|---------------|-------------
885+
|1350|[Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments/)|[Solution](../master/database/_1350.sql) || Easy |
885886
|1341|[Movie Rating](https://leetcode.com/problems/movie-rating/)|[Solution](../master/database/_1341.sql) || Medium |
886887
|1327|[List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)|[Solution](../master/database/_1327.sql) || Easy |
887888
|1322|[Ads Performance](https://leetcode.com/problems/ads-performance/)|[Solution](../master/database/_1322.sql) || Easy |

database/_1350.sql

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--1350. Students With Invalid Departments
2+
--
3+
--Table: Departments
4+
--
5+
--+---------------+---------+
6+
--| Column Name | Type |
7+
--+---------------+---------+
8+
--| id | int |
9+
--| name | varchar |
10+
--+---------------+---------+
11+
--id is the primary key of this table.
12+
--The table has information about the id of each department of a university.
13+
--
14+
--
15+
--Table: Students
16+
--
17+
--+---------------+---------+
18+
--| Column Name | Type |
19+
--+---------------+---------+
20+
--| id | int |
21+
--| name | varchar |
22+
--| department_id | int |
23+
--+---------------+---------+
24+
--id is the primary key of this table.
25+
--The table has information about the id of each student at a university and the id of the department he/she studies at.
26+
--
27+
--
28+
--Write an SQL query to find the id and the name of all students who are enrolled in departments that no longer exists.
29+
--
30+
--Return the result table in any order.
31+
--
32+
--The query result format is in the following example:
33+
--
34+
--Departments table:
35+
--+------+--------------------------+
36+
--| id | name |
37+
--+------+--------------------------+
38+
--| 1 | Electrical Engineering |
39+
--| 7 | Computer Engineering |
40+
--| 13 | Bussiness Administration |
41+
--+------+--------------------------+
42+
--
43+
--Students table:
44+
--+------+----------+---------------+
45+
--| id | name | department_id |
46+
--+------+----------+---------------+
47+
--| 23 | Alice | 1 |
48+
--| 1 | Bob | 7 |
49+
--| 5 | Jennifer | 13 |
50+
--| 2 | John | 14 |
51+
--| 4 | Jasmine | 77 |
52+
--| 3 | Steve | 74 |
53+
--| 6 | Luis | 1 |
54+
--| 8 | Jonathan | 7 |
55+
--| 7 | Daiana | 33 |
56+
--| 11 | Madelynn | 1 |
57+
--+------+----------+---------------+
58+
--
59+
--Result table:
60+
--+------+----------+
61+
--| id | name |
62+
--+------+----------+
63+
--| 2 | John |
64+
--| 7 | Daiana |
65+
--| 4 | Jasmine |
66+
--| 3 | Steve |
67+
--+------+----------+
68+
--
69+
--John, Daiana, Steve and Jasmine are enrolled in departments 14, 33, 74 and 77 respectively. department 14, 33, 74 and 77 doesn't exist in the Departments table.
70+
71+
--# Write your MySQL query statement below
72+
select id, name from Students where department_id not in (select id from Departments);

0 commit comments

Comments
 (0)