Leetcode数据库习题之Mysql实现

1、简单难度

175、组合两个表

select p.FirstName, p.LastName, a.City, a.State 
    from Person p 
    left join Address a 
    on p.PersonId = a.PersonId

这里需要注意,不能使用内连接,内连接只有满足条件的数据才会出现在结果中。
如果people没有地址则没有满足的结果,与题意不符。

176、第二高的薪水

select distinct Salary as SecondHighestSalary 
    from Employee 
    order by Salary DESC limit 1, 1

distinct:去除查询结果中的重复记录。

181、超过经理收入的员工

select e1.Name as Employee 
    from Employee e1, Employee e2 
    where e1.ManagerId = e2.Id and e1.Salary > e2.Salary

182、查找重复的电子邮箱

select Email 
    from Person 
    group by Email having count(Email) > 1

183、从不订购的客户

select Name as Customers 
    from Customers 
    where Id not in (select CustomerId from Orders)

196、删除重复的电子邮箱

delete p2.* 
    from Person p1, Person p2 
    where p1.Email = p2.Email and p2.Id > p1.Id

197、上升的温度

select w2.Id 
    from Weather w1, Weather w2 
    where w2.Temperature > w1.Temperature and DATE_SUB(w2.RecordDate,INTERVAL 1 DAY) = w1.RecordDate

595、大的国家

select name, population, area 
    from World 
    where area > 3000000 or population > 25000000

596、超过5名学生的课

select class 
    from (select distinct * from courses) as a 
    group by a.class 
    having count(class) >= 5

这个题比较坑,一个学生可以上多次相同的课,数据库有多条相同的记录。

620、有趣的电影

select id, movie, description, rating 
    from cinema 
    where description <> 'boring' and MOD(ID, 2) = 1 
    order by rating DESC

627、交换工资

update salary set sex = if(sex = 'm', 'f','m');

Mysql的 IF(Condition, A, B) 函数,条件为true,返回A;条件为false,返回B。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值