目录
一、力扣原题链接
二、题目描述
表:
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)是本表的主键(具有唯一值的列的组合)。 本表的每一行都表示一轮面试的分数编写解决方案,找出 至少有两年 工作经验、且面试分数之和 严格大于
15
的候选人的 ID 。可以以 任何顺序 返回结果表。
查询结果的格式如下例所示。
示例 1:
输入: 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 年工作经验。由于工作年限和分数,不列入结果表。
三、建表语句
drop table if exists Candidates;
drop table if exists Rounds;
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);
Truncate table Candidates;
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');
Truncate table Rounds;
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');
四、题目分析
1、至少有两年 工作经验
Candidates表的years_of_exp大约等于2
2、面试分数之和 大于 15
Rounds表根据interview_id分组聚合求所有人的总分,在筛选大于15
3、关联两种筛选后的表,得出符合要求的candidate_id
五、SQL解答
select
candidate_id
from Candidates c
join Rounds r on c.interview_id = r.interview_id
where years_of_exp >= 2
group by candidate_id
having sum(score) > 15
六、最终答案
select
candidate_id
from Candidates c
join Rounds r on c.interview_id = r.interview_id
where years_of_exp >= 2
group by candidate_id
having sum(score) > 15