力扣SQL仅数据库(610-1050)

610. 判断三角形

需求:对每三个线段报告它们是否可以形成一个三角形。

数据准备:

Create table If Not Exists Triangle (x int, y int, z int)
Truncate table Triangle
insert into Triangle (x, y, z) values ('13', '15', '30')
insert into Triangle (x, y, z) values ('10', '20', '15')

代码实现:

select x,y,z,
case when x+y>z and x+z>y and y+z>x then 'Yes'
else 'No' end  Triangle
from Triangle;

612. 平面上的最近距离

需求:编写解决方案,报告 Point2D 表中任意两点之间的最短距离。保留 2 位小数 。

p1(x1, y1) 和 p2(x2, y2) 这两点之间的距离是 sqrt((x2 - x1)2 + (y2 - y1)2) 。

数据准备:

Create Table If Not Exists Point2D (x int not null, y int not null)
Truncate table Point2D
insert into Point2D (x, y) values ('-1', '-1')
insert into Point2D (x, y) values ('0', '0')
insert into Point2D (x, y) values ('-1', '-2')

代码实现:

将表自连接,连接条件为xy的组合不相等

两点之间的距离建议在网上搜现成的,直接套用即可

with t1 as (select sqrt(power((p1.x-p2.x),2)+power((p1.y-p2.y),2)) aa
            from point2d p1 join point2d p2 on (p1.x,p1.y) <> (p2.x,p2.y))
select round(min(aa),2) shortest from t1;

613. 直线上的最近距离

需求:找到 Point 表中任意两点之间的最短距离。

数据准备:

Create Table If Not Exists Point (x int not null)
Truncate table Point
insert into Point (x) values ('-1')
insert into Point (x) values ('0')
insert into Point (x) values ('2')

代码实现:

与611的解题思路一致

select min(sqrt(power((p1.x-p2.x),2))) shortest 
from point p1 join point p2 on p1.x <> p2.x

进阶:如果 Point 表按 升序排列,如何优化你的解决方案?

select p1.x,p2.x,sqrt(power((p1.x-p2.x),2)) shortest 
from point p1 join point p2 on p1.x <> p2.x 
order by shortest;

614. 二级的关注者

需求:编写一个解决方案来报告 二级用户 及其关注者的数量。

返回按 follower 字典序排序 的结果表。

二级关注者 是指满足以下条件的用户:

  • 关注至少一个用户,
  • 被至少一个用户关注。

数据准备:

Create table If Not Exists Follow (followee varchar(255), follower varchar(255))
Truncate table Follow
insert into Follow (followee, follower) values ('Alice', 'Bob')
insert into Follow (followee, follower) values ('Bob', 'Cena')
insert into Follow (followee, follower) values ('Bob', 'Donald')
insert into Follow (followee, follower) values ('Donald', 'Edward')

代码实现:

select followee follower,count(follower) num from follow 
where followee in (select follower from follow) 
group by followee order by follower;

615. 平均工资:部门与公司比较

需求:找出各个部门员工的平均薪资与公司平均薪资之间的比较结果(更高 / 更低 / 相同)。

数据准备:

Create table If Not Exists Salary (id int, employee_id int, amount int, pay_date date)
Create table If Not Exists Employee (employee_id int, department_id int)
Truncate table Salary
insert into Salary (id, employee_id, amount, pay_date) values ('1', '1', '9000', '2017/03/31')
insert into Salary (id, employee_id, amount, pay_date) values ('2', '2', '6000', '2017/03/31')
insert into Salary (id, employee_id, amount, pay_date) values ('3', '3', '10000', '2017/03/31')
insert into Salary (id, employee_id, amount, pay_date) values ('4', '1', '7000', '2017/02/28')
insert into Salary (id, employee_id, amount, pay_date) values ('5', '2', '6000', '2017/02/28')
insert into Salary (id, employee_id, amount, pay_date) values ('6', '3', '8000', '2017/02/28')
Truncate table Employee
insert into Employee (employee_id, department_id) values ('1', '1')
insert into Employee (employee_id, department_id) values ('2', '2')
insert into Employee (employee_id, department_id) values ('3', '2')

代码实现:

t1 表示将salary表与employee表联合起来,并写出后面所需要用到的列

t2 表示在t1的基础上,计算出各部门各月份的平均薪资

t3 表示在t1的基础上,计算出整个公司各月份的平均薪资

t4 表示联合t2,t3 使用case  when 比较平均薪资的大小

with t1 as (select substring(pay_date,1,7) pay_month,department_id,amount
            from salary s join employee e on s.employee_id=e.employee_id)
,t2 as (select pay_month pay_month1,department_id,sum(amount)/count(department_id) sum_bm from t1 group by pay_month,department_id)
,t3 as (select pay_month pay_month2,sum(amount)/count(department_id) sum_gs from t1 group by pay_month)
select pay_month1 pay_month,department_id,
       case when sum_bm>sum_gs then 'higher'
            when sum_bm=sum_gs then 'same'
        else 'lower' end comparison
from t2 left join t3 on t2.pay_month1=t3.pay_month2;

618.学生地理信息报告

需求:一所学校有来自亚洲、欧洲和美洲的学生。

编写解决方案实现对大洲(continent)列的 透视表 操作,使得每个学生按照姓名的字母顺序依次排列在对应的大洲下面。输出的标题应依次为美洲(America)、亚洲(Asia)和欧洲(Europe)。

测试用例的生成保证来自美国的学生人数不少于亚洲或欧洲的学生人数。

数据准备:

Create table If Not Exists Student (name varchar(50), continent varchar(7))
Truncate table Student
insert into Student (name, continent) values ('Jane', 'America')
insert into Student (name, continent) values ('Pascal', 'Europe')
insert into Student (name, continent) values ('Xi', 'Asia')
insert into Student (name, continent) values ('Jack', 'America')

代码实现:

#不通过

select
    case continent when 'America' then name end America,
    case continent when 'Asia' then name end Asia,
    case continent when 'Europe' then name end Europe
from student;

上面的代码运行后有太多的null值无法过滤掉,因此引用了max函数来去掉空值

with t1 as (select *,row_number() over(partition by continent order by name) con from student)
select
    max(case continent when 'America' then name end) America,
    max(case continent when 'Asia' then name end) Asia,
    max(case continent when 'Europe' then name end) Europe
from t1 group by con;

619. 只出现一次的最大数字

需求:单一数字 是在 MyNumbers 表中只出现一次的数字。

找出最大的 单一数字 。如果不存在 单一数字 ,则返回 null 。

数据准备:

Create table If Not Exists MyNumbers (num int)
Truncate table MyNumbers
insert into MyNumbers (num) values ('8')
insert into MyNumbers (num) values ('8')
insert into MyNumbers (num) values ('3')
insert into MyNumbers (num) values ('3')
insert into MyNumbers (num) values ('1')
insert into MyNumbers (num) values ('4')
insert into MyNumbers (num) values ('5')
insert into MyNumbers (num) values ('6')

代码实现:

使用max实现当数据为空时,保留空值null

with t1 as (select num,count(num)con from mynumbers group by num)
 select max(num) num from t1 where con=1 order by num desc limit 1;

620. 有趣的电影

需求:编写解决方案,找出所有影片描述为  boring (不无聊) 的并且 id 为奇数 的影片。

返回结果按 rating 降序排列

数据来源:

Create table If Not Exists cinema (id int, movie varchar(255), description varchar(255), rating float(2, 1))
Truncate table cinema
insert into cinema (id, movie, description, rating) values ('1', 'War', 'great 3D', '8.9')
insert into cinema (id, movie, description, rating) values ('2', 'Science', 'fiction', '8.5')
insert into cinema (id, movie, description, rating) values ('3', 'irish', 'boring', '6.2')
insert into cinema (id, movie, description, rating) values ('4', 'Ice song', 'Fantacy', '8.6')
insert into cinema (id, movie, description, rating) values ('5', 'House card', 'Interesting', '9.1')

代码实现:

select * from cinema 
where description != 'boring' and id%2=1 order by rating desc;

626. 换座位

需求:编写解决方案来交换每两个连续的学生的座位号。如果学生的数量是奇数,则最后一个学生的id不交换。 按 id 升序 返回结果表

数据准备:

Create table If Not Exists Seat (id int, student varchar(255))
Truncate table Seat
insert into Seat (id, student) values ('1', 'Abbot')
insert into Seat (id, student) values ('2', 'Doris')
insert into Seat (id, student) values ('3', 'Emerson')
insert into Seat (id, student) values ('4', 'Green')
insert into Seat (id, student) values ('5', 'Jeames')

代码实现:

t1 将奇数、偶数的 id 调换

再使用排序将最后一位是奇数位时的 id 号排回来

with t1 as (select *,case when id%2=1 then id+1
                        when id%2=0 then id-1
                        end rou
            from seat order by rou)
select rank()over(order by rou) id,student from t1;

627. 变更性别

需求:请你编写一个解决方案来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。

注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。

数据准备:

Create table If Not Exists Salary (id int, name varchar(100), sex char(1), salary int)
Truncate table Salary
insert into Salary (id, name, sex, salary) values ('1', 'A', 'm', '2500')
insert into Salary (id, name, sex, salary) values ('2', 'B', 'f', '1500')
insert into Salary (id, name, sex, salary) values ('3', 'C', 'm', '5500')
insert into Salary (id, name, sex, salary) values ('4', 'D', 'f', '500')

代码实现:

update salary set sex=case sex when 'm' then 'f' else 'm' end;

1045. 买下所有产品的客户 

需求:编写解决方案,报告 Customer 表中购买了 Product 表中所有产品的客户的 id。

数据来源:

Create table If Not Exists Customer (customer_id int, product_key int)
Create table Product (product_key int)
Truncate table Customer
insert into Customer (customer_id, product_key) values ('1', '5')
insert into Customer (customer_id, product_key) values ('2', '6')
insert into Customer (customer_id, product_key) values ('3', '5')
insert into Customer (customer_id, product_key) values ('3', '6')
insert into Customer (customer_id, product_key) values ('1', '6')
Truncate table Product
insert into Product (product_key) values ('5')
insert into Product (product_key) values ('6')

代码实现:

select customer_id from customer group by customer_id
having count(distinct product_key)=(select count(product_key) from product);

 拓展:如果存在有customer表中的product_key的值不属于product表呢?

with t1 as (select distinct customer_id, product_key from customer 
                where product_key in (select product_key from product))
select customer_id from t1 group by customer_id 
having count(product_key)=(select count(product_key) from product);

1050. 合作过至少三次的演员和导演

需求:编写解决方案找出合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

数据来源:

Create table If Not Exists ActorDirector (actor_id int, director_id int, timestamp int)
Truncate table ActorDirector
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '0')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '1')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '1', '2')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '2', '3')
insert into ActorDirector (actor_id, director_id, timestamp) values ('1', '2', '4')
insert into ActorDirector (actor_id, director_id, timestamp) values ('2', '1', '5')
insert into ActorDirector (actor_id, director_id, timestamp) values ('2', '1', '6')

代码实现:

select actor_id,director_id from actordirector 
group by actor_id, director_id having count(timestamp)>=3;
  • 24
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值