力扣数据库13个题目题解(萌新做题第二天)

俩天摸鱼的成功


解析开始


175. 组合两个表

考点:

连接:1.from后面写表 2.join 表 on 条件 3.left join  表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

# Write your MySQL query statement below
select firstName,lastName,city,state as state
from Person left outer join Address
on Person.PersonId=Address.PersonId   ;

第二高的薪水

 考点:

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

ifnull函数的使用 ifnull(  context1,context2):如果context1是空则返回context2否则返回context1

limit函数的使用:

limit n子句表示查询结果返回前n条数据

offset n表示跳过x条语句

limit y offset x 分句表示查询结果跳过 x 条数据,读取前 y 条数据

使用limit和offset,降序排列再返回第二条记录可以得到第二大的值。

distinct:去重复

select ifnull(
(
    select  distinct salary
from    Employee 
order by salary DESC
limit 1, 1
),
null 
) as SecondHighestSalary 

177. 第N高的薪水

考点:

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

ifnull函数的使用 ifnull( context1,context2):如果context1是空则返回context2否则返回context1

limit函数的使用:

limit n子句表示查询结果返回前n条数据

offset n表示跳过x条语句

limit y offset x 分句表示查询结果跳过 x 条数据,读取前 y 条数据

使用limit和offset,降序排列再返回第二条记录可以得到第二大的值。

distinct:去重复

declare:定义常量列如declare 名字 类型名

order by ;排序,默认asc升序,desc降序

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare num Int;
set num=N-1;

  RETURN (
    ifnull(
        (
            select distinct salary
            from Employee
            order by salary DESC
            limit num,1
        ),
        null
    ) 
      
  );
END

178. 分数排名

考点:

自定义名字:1.直接在后面写记得空一格,2.as

rank用法参考链接:https://blog.csdn.net/qq_41057885/article/details/109176014

select score,dense_rank() over(order by score desc) as 'rank'
from scores

180. 连续出现的数字

题解:https://leetcode.cn/problems/consecutive-numbers/solutions/21537/sql-server-jie-fa-by-neilsons/

181. 超过经理收入的员工

考点

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

思路:俩个表进行查询

SELECT
     a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
     ON a.ManagerId = b.Id
     AND a.Salary > b.Salary

183. 从不订购的客户

考点

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

not in:不包含的意思

思路:先查找出买过的,然后not in将他排除

select Name as Customers
from Customers 
where Id  not in(

select  Customers.Id 
from Customers  join Orders
on Customers.Id=Orders.CustomerId 
)

184. 部门工资最高的员工

考点

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

思路:先求出该部门工资最高的是多少,然后再拿最高工资和本部门工资比较

select Department.name as 'Department',Employee.name as 'Employee',Employee.Salary 
from Employee ,Department,(
    select departmentId ,max(Salary) as m
from Employee
group by departmentId
)as test

where Employee.departmentId=Department .id and Employee .departmentId=test.departmentId and Employee.Salary=test.m 

185. 部门工资前三高的所有员工

考点:

连接:1.from后面写表 2.join 表 on 条件 3.left join 表 on条件 4.right join 表 on条件

自定义名字:1.直接在后面写记得空一格,2.as

distinct:去重复

思路:

先连接部门号对应的部门名字,然后工资前三意思就是说改工资最多可以小于二个工资

select Department.name as 'Department',e1.name as 'Employee' ,e1.Salary as 'Salary'
from Employee as e1,Department
where 
e1.DepartmentId=Department.Id
and 
3>(
    select  count(distinct e2.Salary)
    from Employee as e2
    where e1.departmentId =e2.departmentId  and e1.Salary<e2.Salary

)

196. 删除重复的电子邮箱

考点

delete:

在DELETE官方文档中,给出了这一用法,比如下面这个DELETE语句👇

DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;

这种DELETE方式很陌生,竟然和SELETE的写法类似。它涉及到t1和t2两张表,DELETE t1表示要删除t1的一些记录,具体删哪些,就看WHERE条件,满足就删;

这里删的是t1表中,跟t2匹配不上的那些记录。

思路:邮箱必须相等你并且我要删除的内容id要大于我对比的id

delete p1
from Person as p1,Person p2
where p1.id>p2.id and p1.email = p2.email;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值