SQL刷题记

目录

175. 组合两个表

知识点:左外连接

select firstName,lastName,city,state
from Person Left OUTER JOIN Address ON(Person.personId = Address.personId)

176. 第二高的薪水

本题用MYSQL做,则可以利用limit和offset语句

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

SQL解决代码如下

select max(distinct salary) as SecondHighestSalary
from Employee
where salary < (
    select max(distinct salary)
    from Employee
)

180. 连续出现的数字

三表连接

/* Write your T-SQL query statement below */
select distinct l1.Num as ConsecutiveNums
from Logs as l1,Logs as l2,Logs as l3
where l1.Id=l2.Id-1 and l2.Id=l3.Id-1 and l1.Num = l2.Num and l2.Num = l3.Num

181. 超过经理收入的员工

自身连接

/* Write your T-SQL query statement below */
select e1.name as Employee
from Employee as e1,Employee as e2
where e1.managerId = e2.id and e1.salary > e2.salary

1873. 计算特殊奖金

使用union合并两种结果

/* Write your T-SQL query statement below */
select employee_id,salary as bonus
from Employees
where employee_id%2 = 1 and name not like('M%')
union 
select employee_id,salary*0 as bonus
from Employees
where employee_id%2 = 0 or name like('M%')

196. 删除重复的电子邮箱

嵌套查询

/* 
 Please write a DELETE statement and DO NOT write a SELECT statement.
 Write your T-SQL query statement below
 */
delete
from Person
where id not in(
    select min(P.id)
    from Person as P
    group by P.email
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容艾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值