LeetCode-数据库-MySQL题解(11-20)

题目来自于leetcode,数据库模块,可以一起练习:LeetCode-database-OJ

196. 删除重复的电子邮箱

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

Id Email
1 [email protected]
2 [email protected]
3 [email protected]

Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

Id Email
1 [email protected]
2 [email protected]

提示:
执行 SQL 之后,输出是整个 Person 表。
使用 delete 语句。

DELETE p1 
FROM Person p1,Person p2
WHERE p1.Email = p2.Email AND p1.Id > p2.Id

197. 上升的温度

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

Id(INT) RecordDate(DATE) Temperature(INT)
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

例如,根据上述给定的 Weather 表格,返回如下 Id:

Id
2
4
select w1.Id
from Weather w1,Weather w2
where datediff(w1.RecordDate, w2.RecordDate) = 1 and w2.Temperature<w1.Temperature

262. 行程和用户

Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

Id Client_Id Driver_Id City_Id Status Request_at
1 1 10 1 completed 2013-10-01
2 2 11 1 cancelled_by_driver 2013-10-01
3 3 12 6 completed 2013-10-01
4 4 13 6 cancelled_by_client 2013-10-01
5 1 10 1 completed 2013-10-02
6 2 11 6 completed 2013-10-02
7 3 12 6 completed 2013-10-02
8 2 12 12 completed 2013-10-03
9 3 10 12 completed 2013-10-03
10 4 13 12 cancelled_by_driver 2013-10-03

Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

Users_Id Banned Role
1 No client
2 Yes client
3 No client
4 No client
10 No driver
11 No driver
12 No driver
13 No driver

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。
取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)

Day Cancellation Rate
2013-10-01 0.33
2013-10-02 0.00
2013-10-03 0.50

595. 大的国家

这里有张 World 表

name continent area population gdp
Afghanistan Asia 652230 25500100 20343000
Albania Europe 28748 2831741 12960000
Algeria Africa 2381741 37100000 188681000
Andorra Europe 468 78115 3712000
Angola Africa 1246700 20609294 100990000

如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。
编写一个SQL查询,输出表中所有大国家的名称、人口和面积。
例如,根据上表,我们应该输出:

name population area
Afghanistan 25500100 652230
Algeria 37100000 2381741
SELECT `name`,population,`area` 
FROM World
WHERE population>=25000000 or `area`>=3000000
SELECT name, population, area
FROM world
WHERE area > 3000000
UNION
SELECT name, population, area
FROM world
WHERE population > 25000000

596. 超过5名学生的课

有一个courses 表 ,有: student (学生) 和 class (课程)。
请列出所有超过或等于5名学生的课。

student class
A Math
B English
C Math
D Biology
E Math
F Computer
G Math
H Math
I Math

输出:

class
Math

Note:
学生在每个课中不应被重复计算

SELECT class
FROM courses
GROUP BY class
HAVING COUNT(distinct(student))>=5

601. 体育馆的人流量

X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)。
请编写一个查询语句,找出人流量的高峰期。高峰期时,至少连续三行记录中的人流量不少于100。
例如,表 stadium:

id visit_date people
1 2017-01-01 10
2 2017-01-02 109
3 2017-01-03 150
4 2017-01-04 99
5 2017-01-05 145
6 2017-01-06 1455
7 2017-01-07 199
8 2017-01-08 188

对于上面的示例数据,输出为:

id visit_date people
5 2017-01-05 145
6 2017-01-06 1455
7 2017-01-07 199
8 2017-01-08 188

提示:
每天只有一行记录,日期随着 id 的增加而增加。

SELECT DISTINCT s4.id,s4.visit_date,s4.people 
FROM stadium s1,stadium s2,stadium s3,stadium s4 
WHERE s1.id+1=s2.id AND s2.id+1=s3.id 
AND s1.people>=100 AND s2.people>=100 AND s3.people>=100 
AND s4.id IN (s1.id,s2.id,s3.id)

620. 有趣的电影

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。
作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。
例如,下表 cinema:

id movie description rating
1 War great 3D 8.9
2 Science fiction 8.5
3 irish boring 6.2
4 Ice song Fantacy 8.6
5 House card Interesting 9.1

对于上面的例子,则正确的输出是为:

id movie description rating
5 House card Interesting 9.1
1 War great 3D 8.9
SELECT *
FROM cinema
WHERE id%2 <> 0 and description <> 'boring'
ORDER BY rating DESC

626. 换座位

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
示例:

id student
1 Abbot
2 Doris
3 Emerson
4 Green
5 Jeames

假如数据输入的是上表,则输出结果如下:

id student
1 Doris
2 Abbot
3 Green
4 Emerson
5 Jeames

注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。

select (case when mod(id,2) = 1 and  id = (select count(id) from seat) then id
	when mod(id,2) = 1 then id + 1            
	else id - 1 end)id,student from Seat
order by id

627. 交换工资

给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。
注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。

id name sex salary
1 A m 2500
2 B f 1500
3 C m 5500
4 D f 500

运行你所编写的更新语句之后,将会得到以下表:

id name sex salary
1 A f 2500
2 B m 1500
3 C f 5500
4 D m 500
UPDATE salary
SET sex = IF(sex = 'm','f','m')
UPDATE salary 
SET sex = CASE sex         
	  WHEN "m" THEN "f"         
	  ELSE "m"     
	END;

1179. 重新格式化部门表

部门表 Department:

Column Name Type
id int
revenue int
month varchar

(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 [“Jan”,“Feb”,“Mar”,“Apr”,“May”,“Jun”,“Jul”,“Aug”,“Sep”,“Oct”,“Nov”,“Dec”]。
编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。
查询结果格式如下面的示例所示:
Department 表:

id revenue month
1 8000 Jan
2 9000 Jan
3 10000 Feb
1 7000 Feb
1 6000 Mar

查询得到的结果表:

id Jan_Revenue Feb_Revenue Mar_Revenue Dec_Revenue
1 8000 7000 6000 null
2 9000 null null null
3 null 10000 null null

注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。

select id, 
sum(case `month` when 'Jan' then revenue else null end) as Jan_Revenue, 
sum(case `month` when 'Feb' then revenue else null end) as Feb_Revenue, 
sum(case `month` when 'Mar' then revenue else null end) as Mar_Revenue, 
sum(case `month` when 'Apr' then revenue else null end) as Apr_Revenue, 
sum(case `month` when 'May' then revenue else null end) as May_Revenue, 
sum(case `month` when 'Jun' then revenue else null end) as Jun_Revenue, 
sum(case `month` when 'Jul' then revenue else null end) as Jul_Revenue, 
sum(case `month` when 'Aug' then revenue else null end) as Aug_Revenue, 
sum(case `month` when 'Sep' then revenue else null end) as Sep_Revenue, 
sum(case `month` when 'Oct' then revenue else null end) as Oct_Revenue, 
sum(case `month` when 'Nov' then revenue else null end) as Nov_Revenue, 
sum(case `month` when 'Dec' then revenue else null end) as Dec_Revenue
from Department 
group by id

1 - 10 题

1 - 10题

猜你喜欢

转载自blog.csdn.net/weixin_43845524/article/details/106630135
今日推荐