题目描述

(通过次数1,643 | 提交次数2,152,通过率76.35%)

?表:Candidates
+--------------+----------+
| Column Name  | Type     |
+--------------+----------+
| candidate_id | int      |
| name         | varchar  |
| years_of_exp | int      |
| interview_id | int      |
+--------------+----------+
candidate_id 是这个表的主键。
该表的每一行都表示候选人的姓名、工作年限以及面试 ID 。
表:Rounds
+--------------+------+
| Column Name  | Type |
+--------------+------+
| interview_id | int  |
| round_id     | int  |
| score        | int  |
+--------------+------+
(interview_id, round_id)是本表的主键。
本表的每一行都表示一轮面试的分数
编写一个 SQL 查询,查询出至少有两年工作经验、且面试分数之和 严格大于 15的候选人的 ID 。
可以以 任何顺序 返回结果表。
查询结果的格式如下例所示。
示例:
输入:
Candidates table:
+--------------+---------+--------------+--------------+
| candidate_id | name    | years_of_exp | interview_id |
+--------------+---------+--------------+--------------+
| 11           | Atticus | 1            | 101          |
| 9            | Ruben   | 6            | 104          |
| 6            | Aliza   | 10           | 109          |
| 8            | Alfredo | 0            | 107          |
+--------------+---------+--------------+--------------+
Rounds table:
+--------------+----------+-------+
| interview_id | round_id | score |
+--------------+----------+-------+
| 109          | 3        | 4     |
| 101          | 2        | 8     |
| 109          | 4        | 1     |
| 107          | 1        | 3     |
| 104          | 3        | 6     |
| 109          | 1        | 4     |
| 104          | 4        | 7     |
| 104          | 1        | 2     |
| 109          | 2        | 1     |
| 104          | 2        | 7     |
| 107          | 2        | 3     |
| 101          | 1        | 8     |
+--------------+----------+-------+
输出:
+--------------+
| candidate_id |
+--------------+
| 9            |
+--------------+
解释:
- 候选人 11 :总分是 16 ,1 年工作经验。由于工作年限,不列入结果表。
- 候选人 9 :总分是 22 ,6 年工作经验。列入结果表。
- 候选人 6 :总分是 10 ,10 年工作经验。由于分数不足,不列入结果表。
- 候选人 8 :总分是 6 ,0 年工作经验。由于工作年限和分数,不列入结果表。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/accepted-candidates-from-the-interviews
//测试数据
Create table If Not Exists Candidates (candidate_id int, name varchar(30), years_of_exp int, interview_id int);
Create table If Not Exists Rounds (interview_id int, round_id int, score int);

insert into Candidates (candidate_id, name, years_of_exp, interview_id) values ('11', 'Atticus', '1', '101');
insert into Candidates (candidate_id, name, years_of_exp, interview_id) values ('9', 'Ruben', '6', '104');
insert into Candidates (candidate_id, name, years_of_exp, interview_id) values ('6', 'Aliza', '10', '109');
insert into Candidates (candidate_id, name, years_of_exp, interview_id) values ('8', 'Alfredo', '0', '107');

insert into Rounds (interview_id, round_id, score) values ('109', '3', '4');
insert into Rounds (interview_id, round_id, score) values ('101', '2', '8');
insert into Rounds (interview_id, round_id, score) values ('109', '4', '1');
insert into Rounds (interview_id, round_id, score) values ('107', '1', '3');
insert into Rounds (interview_id, round_id, score) values ('104', '3', '6');
insert into Rounds (interview_id, round_id, score) values ('109', '1', '4');
insert into Rounds (interview_id, round_id, score) values ('104', '4', '7');
insert into Rounds (interview_id, round_id, score) values ('104', '1', '2');
insert into Rounds (interview_id, round_id, score) values ('109', '2', '1');
insert into Rounds (interview_id, round_id, score) values ('104', '2', '7');
insert into Rounds (interview_id, round_id, score) values ('107', '2', '3');
insert into Rounds (interview_id, round_id, score) values ('101', '1', '8');

解题思路

Candidates表保存了所有的候选人信息。
Rounds表保存了候选人在每轮面试中的得分。
题目要求:查询至少有2年工作经验且面试总得分在15分以上的候选人。
很明显,查询出的候选人需要同时满足两个条件。
对于第一个条件,直接从Candidates表中筛选即可。
对于第二个条件,Rounds表中保存的是每一轮的得分明细,而题目要求按总得分筛选。所以需要先对Rounds表做下汇总统计,然后再筛选过滤。
最后,使用inner join关联出同时满足两个条件的候选人。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
with
tmp as (
    select
        interview_id,
        sum(score) score
    from Rounds
    group by interview_id
    having sum(score) > 15
)
select
    a.candidate_id
from Candidates a
inner join tmp b
on a.interview_id = b.interview_id
where a.years_of_exp >=2;
picture loss