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

2024-09-02 05:28

本文主要是介绍力扣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)) aafrom 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,amountfrom 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')

代码实现:

#不通过

selectcase 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)
selectmax(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+1when id%2=0 then id-1end roufrom 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;

这篇关于力扣SQL仅数据库(610-1050)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1129116

相关文章

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

PostgreSQL数据库密码被遗忘时的操作步骤

《PostgreSQL数据库密码被遗忘时的操作步骤》密码遗忘是常见的用户问题,因此提供一种安全的遗忘密码找回机制是十分必要的,:本文主要介绍PostgreSQL数据库密码被遗忘时的操作步骤的相关资... 目录前言一、背景知识二、Windows环境下的解决步骤1. 找到PostgreSQL安装目录2. 修改p

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名