1. 牛客SQL热题255:给出employees表中排名为奇数行的first_name
1.1 题目:
描述
对于employees表中,输出first_name排名(按first_name升序排序)为奇数的first_name
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
如,输入为:
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');
INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
输出格式:
first |
Georgi |
Anneke |
请你在不打乱原序列顺序的情况下,输出:按first_name排升序后,取奇数行的first_name。
如对以上示例数据的first_name排序后的序列为:Anneke、Bezalel、Georgi、Kyoichi。
则原序列中的Georgi排名为3,Anneke排名为1,所以按原序列顺序输出Georgi、Anneke。
示例1
输入:
drop table if exists `employees` ;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');
INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
复制输出:
Georgi
Anneke
1.2 思路:
看注释。
1.3 题解:
with tep1 as (
-- 先生成依据first_name排序后的临时表
select first_name, rank() over (order by first_name) ranks
from employees
), tep2 as (
-- 筛选出来排名为奇数的姓名
select first_name
from tep1
where ranks % 2 = 1
)
-- 输出当然是依据原表
select first_name first
from employees
where first_name in (select * from tep2)
2. 牛客SQL热题253:获取有奖金的员工相关信息。
2.1 题目:
描述
现有员工表employees如下:
emp_no | birth_date | first_name | last_name | gender | hire_date |
10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 |
10002 | 1964-06-02 | Bezalel | Simmel | F | 1985-11-21 |
有员工奖金表emp_bonus:
emp_no | recevied | btype |
10001 | 2010-01-01 | 1 |
10002 | 2010-10-01 | 2 |
有薪水表salaries:
emp_no | salary | from_date | to_date |
10001 | 60117 | 1986-06-26 | 1987-06-26 |
10001 | 62102 | 1987-06-26 | 1988-06-25 |
10001 | 66074 | 1988-06-25 | 1989-06-25 |
10001 | 66596 | 1989-06-25 | 1990-06-25 |
10001 | 66961 | 1990-06-25 | 1991-06-25 |
10001 | 71046 | 1991-06-25 | 1992-06-24 |
10001 | 74333 | 1992-06-24 | 1993-06-24 |
10001 | 75286 | 1993-06-24 | 1994-06-24 |
10001 | 75994 | 1994-06-24 | 1995-06-24 |
10001 | 76884 | 1995-06-24 | 1996-06-23 |
10001 | 80013 | 1996-06-23 | 1997-06-23 |
10001 | 81025 | 1997-06-23 | 1998-06-23 |
10001 | 81097 | 1998-06-23 | 1999-06-23 |
10001 | 84917 | 1999-06-23 | 2000-06-22 |
10001 | 85112 | 2000-06-22 | 2001-06-22 |
10001 | 85097 | 2001-06-22 | 2002-06-22 |
10001 | 88958 | 2002-06-22 | 9999-01-01 |
10002 | 72527 | 1996-08-03 | 1997-08-03 |
10002 | 72527 | 1997-08-03 | 1998-08-03 |
10002 | 72527 | 1998-08-03 | 1999-08-03 |
10002 | 72527 | 1999-08-03 | 2000-08-02 |
10002 | 72527 | 2000-08-02 | 2001-08-02 |
10002 | 72527 | 2001-08-02 | 9999-01-01 |
- 其中bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 to_date='9999-01-01'表示当前薪水。
- 请你给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。
- bonus结果保留一位小数,输出结果按emp_no升序排序。
以上数据集的输出结果如下:
emp_no | first_name | last_name | btype | salary | bonus |
10001 | Georgi | Facello | 1 | 88958 | 8895.8000 |
10002 | Bezalel | Simmel | 2 | 72527 | 14505.4000 |
示例1
输入:
drop table if exists `employees` ;
drop table if exists emp_bonus;
drop table if exists `salaries` ;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
insert into emp_bonus values
(10001, '2010-01-01',1),
(10002, '2010-10-01',2);
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO salaries VALUES(10001,60117,'1986-06-26','1987-06-26');
INSERT INTO salaries VALUES(10001,62102,'1987-06-26','1988-06-25');
INSERT INTO salaries VALUES(10001,66074,'1988-06-25','1989-06-25');
INSERT INTO salaries VALUES(10001,66596,'1989-06-25','1990-06-25');
INSERT INTO salaries VALUES(10001,66961,'1990-06-25','1991-06-25');
INSERT INTO salaries VALUES(10001,71046,'1991-06-25','1992-06-24');
INSERT INTO salaries VALUES(10001,74333,'1992-06-24','1993-06-24');
INSERT INTO salaries VALUES(10001,75286,'1993-06-24','1994-06-24');
INSERT INTO salaries VALUES(10001,75994,'1994-06-24','1995-06-24');
INSERT INTO salaries VALUES(10001,76884,'1995-06-24','1996-06-23');
INSERT INTO salaries VALUES(10001,80013,'1996-06-23','1997-06-23');
INSERT INTO salaries VALUES(10001,81025,'1997-06-23','1998-06-23');
INSERT INTO salaries VALUES(10001,81097,'1998-06-23','1999-06-23');
INSERT INTO salaries VALUES(10001,84917,'1999-06-23','2000-06-22');
INSERT INTO salaries VALUES(10001,85112,'2000-06-22','2001-06-22');
INSERT INTO salaries VALUES(10001,85097,'2001-06-22','2002-06-22');
INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'1996-08-03','1997-08-03');
INSERT INTO salaries VALUES(10002,72527,'1997-08-03','1998-08-03');
INSERT INTO salaries VALUES(10002,72527,'1998-08-03','1999-08-03');
INSERT INTO salaries VALUES(10002,72527,'1999-08-03','2000-08-02');
INSERT INTO salaries VALUES(10002,72527,'2000-08-02','2001-08-02');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
复制输出:
10001|Georgi|Facello|1|88958|8895.8
10002|Bezalel|Simmel|2|72527|14505.4
2.2 思路:
简单的多表连接题。
2.3 题解:
with tep1 as (
-- 先得到当前薪水的记录
select emp_no, salary
from salaries
where to_date = '9999-01-01'
), tep2 as (
-- 多表连接
select t1.emp_no, btype, salary, first_name, last_name
from employees t1
join emp_bonus t2
on t1.emp_no = t2.emp_no
join tep1 t3
on t1.emp_no = t3.emp_no
)
-- case when完事
select emp_no, first_name, last_name, btype, salary,
case btype when 1 then round(salary*0.1, 4)
when 2 then round(salary*0.2, 4)
else round(salary*0.3, 4)
end bonus
from tep2
3. 牛客SQL热题256:出现三次以上相同积分的情况
3.1 题目:
描述
在牛客刷题的小伙伴们都有着牛客积分,积分(grade)表简化可以如下:
id | number |
1 2 3 4 5 |
111 333 111 111 333 |
id为用户主键id,number代表积分情况,让你写一个sql查询,积分表里面出现三次以及三次以上的积分,查询结果如下:
111 |
注意:若有多个符合条件的number,则按number升序排序输出。
示例1
输入:
drop table if exists grade;
CREATE TABLE `grade` (
`id` int(4) NOT NULL,
`number` int(4) NOT NULL,
PRIMARY KEY (`id`));
INSERT INTO grade VALUES
(1,111),
(2,333),
(3,111),
(4,111),
(5,333);
复制输出:
111
3.2 思路:
分组过滤。
3.3 题解:
select number
from grade
group by number
having count(*) >= 3
4. 力扣SQL1294:不同国家的天气类型
4.1 题目:
表:Countries
+---------------+---------+ | Column Name | Type | +---------------+---------+ | country_id | int | | country_name | varchar | +---------------+---------+ country_id 是这张表的主键(具有唯一值的列)。 该表的每行有 country_id 和 country_name 两列。
表:Weather
+---------------+---------+ | Column Name | Type | +---------------+---------+ | country_id | int | | weather_state | varchar | | day | date | +---------------+---------+ (country_id, day) 是该表的复合主键(具有唯一值的列)。 该表的每一行记录了某个国家某一天的天气情况。
编写解决方案找到表中每个国家在 2019 年 11 月的天气类型。
天气类型的定义如下:
- 当
weather_state
的平均值小于或等于15
返回 Cold, - 当
weather_state
的平均值大于或等于25
返回 Hot, - 否则返回 Warm。
以 任意顺序 返回你的查询结果。
返回结果格式如下所示:
示例 1:
输入: Countries table: +------------+--------------+ | country_id | country_name | +------------+--------------+ | 2 | USA | | 3 | Australia | | 7 | Peru | | 5 | China | | 8 | Morocco | | 9 | Spain | +------------+--------------+ Weather table: +------------+---------------+------------+ | country_id | weather_state | day | +------------+---------------+------------+ | 2 | 15 | 2019-11-01 | | 2 | 12 | 2019-10-28 | | 2 | 12 | 2019-10-27 | | 3 | -2 | 2019-11-10 | | 3 | 0 | 2019-11-11 | | 3 | 3 | 2019-11-12 | | 5 | 16 | 2019-11-07 | | 5 | 18 | 2019-11-09 | | 5 | 21 | 2019-11-23 | | 7 | 25 | 2019-11-28 | | 7 | 22 | 2019-12-01 | | 7 | 20 | 2019-12-02 | | 8 | 25 | 2019-11-05 | | 8 | 27 | 2019-11-15 | | 8 | 31 | 2019-11-25 | | 9 | 7 | 2019-10-23 | | 9 | 3 | 2019-12-23 | +------------+---------------+------------+ 输出: +--------------+--------------+ | country_name | weather_type | +--------------+--------------+ | USA | Cold | | Austraila | Cold | | Peru | Hot | | China | Warm | | Morocco | Hot | +--------------+--------------+ 解释: USA 11 月的平均 weather_state 为 (15) / 1 = 15 所以天气类型为 Cold。 Australia 11 月的平均 weather_state 为 (-2 + 0 + 3) / 3 = 0.333 所以天气类型为 Cold。 Peru 11 月的平均 weather_state 为 (25) / 1 = 25 所以天气类型为 Hot。 China 11 月的平均 weather_state 为 (16 + 18 + 21) / 3 = 18.333 所以天气类型为 Warm。 Morocco 11 月的平均 weather_state 为 (25 + 27 + 31) / 3 = 27.667 所以天气类型为 Hot。 我们并不知道 Spain 在 11 月的 weather_state 情况所以无需将他包含在结果中。
4.2 思路:
easy题。
4.3 题解:
with tep1 as (
select country_id , avg(weather_state) avg_wea
from Weather
where substring(day, 1, 7) = '2019-11'
group by country_id
)
select country_name ,
case when avg_wea <= 15 then 'Cold'
when avg_wea >= 25 then 'Hot'
else 'Warm'
end weather_type
from tep1 t1
join Countries t2
on t1.country_id = t2.country_id