题目描述

(通过次数12,105 | 提交次数14,259,通过率84.89%)

院系表: Departments
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
id 是该表的主键
该表包含一所大学每个院系的 id 信息
学生表: Students
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
| department_id | int     |
+---------------+---------+
id 是该表的主键
该表包含一所大学每个学生的 id 和他/她就读的院系信息

写一条 SQL 语句以查询那些所在院系不存在的学生的 id 和姓名
可以以 任何顺序 返回结果。
下面是返回结果格式的例子。
示例 1:
输入:
Departments 表:
+------+--------------------------+
| id   | name                     |
+------+--------------------------+
| 1    | Electrical Engineering   |
| 7    | Computer Engineering     |
| 13   | Bussiness Administration |
+------+--------------------------+
Students 表:
+------+----------+---------------+
| 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             |
+------+----------+---------------+
输出:
+------+----------+
| id   | name     |
+------+----------+
| 2    | John     |
| 7    | Daiana   |
| 4    | Jasmine  |
| 3    | Steve    |
+------+----------+
解释:
John, Daiana, Steve 和 Jasmine 所在的院系分别是 14, 33, 74 和 77, 其中 14, 33, 74 和 77 并不存在于院系表

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/students-with-invalid-departments
//测试数据
Create table If Not Exists Departments (id int, name varchar(30));
Create table If Not Exists Students (id int, name varchar(30), department_id int);

insert into Departments (id, name) values ('1', 'Electrical Engineering');
insert into Departments (id, name) values ('7', 'Computer Engineering');
insert into Departments (id, name) values ('13', 'Bussiness Administration');

insert into Students (id, name, department_id) values ('23', 'Alice', '1');
insert into Students (id, name, department_id) values ('1', 'Bob', '7');
insert into Students (id, name, department_id) values ('5', 'Jennifer', '13');
insert into Students (id, name, department_id) values ('2', 'John', '14');
insert into Students (id, name, department_id) values ('4', 'Jasmine', '77');
insert into Students (id, name, department_id) values ('3', 'Steve', '74');
insert into Students (id, name, department_id) values ('6', 'Luis', '1');
insert into Students (id, name, department_id) values ('8', 'Jonathan', '7');
insert into Students (id, name, department_id) values ('7', 'Daiana', '33');
insert into Students (id, name, department_id) values ('11', 'Madelynn', '1');

解题思路

Departments表保存了所有的部门信息。

Students表保存了所有的学生信息,包括学生所在的院系。

题目要求:查询所在的院系不存在的学生。

院系不存在的意思是说,Students表中的department_id不存在于Departments表中。

对于查询“不存在”场景的写法有多种。最常见的就是使用IN/NOT IN、EXISTS/NOT EXISTS子句。

当然,鉴于左关联的特性,当左表与右表关联不上时,右表的值为NULL,也可以用来查询“不存在”的场景。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
--方法一:
select
    a.id,
    a.name
from Students a
where not exists(
    select 1
    from Departments b
    where a.department_id = b.id
);

--方法二:
select
    a.id,
    a.name
from Students a
left join Departments b
on a.department_id = b.id
where b.id is null;
picture loss