俩天摸鱼的成功
![](https://img-blog.csdnimg.cn/img_convert/17f64fce1aa51d6e6788042ffaa5d168.png)
解析开始
考点:
连接: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
考点:
连接: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
考点:
自定义名字: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
题解:https://leetcode.cn/problems/consecutive-numbers/solutions/21537/sql-server-jie-fa-by-neilsons/
考点
连接: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
考点
连接: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
)
考点
连接: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
考点:
连接: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
)
考点
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;