leetcode SQL 学习

1.有趣的电影

# Write your MySQL query statement below
select *
from cinema
where id % 2 = 1 and description != "boring"
order by rating desc;

2.寻找用户推荐人

# Write your MySQL query statement below
select name 
from customer
where referee_id <> 2 or referee_id IS NULL

3.订单最多的客户

# Write your MySQL query statement below
select customer_number
from Orders
group by 
customer_number
order by
count(customer_number) desc
limit 1

4.组合两个表

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

5.第二高的薪水

select max(salary) as Secondhighestsalary
from employee
where salary<(select max(salary) from employee)

6.查找重复的电子邮箱

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

7.从不订购的客户

# Write your MySQL query statement below
select name as Customers
from Customers
where id not in
(select customerId from orders)

8.部门工资最高的员工

# Write your MySQL query statement below
select d.name as Department,
       e.name as Employee,
       e.salary as Salary 
from Employee e,Department d 
where e.departmentId = d.id
and (e.salary,e.departmentId)
in(select max(salary),departmentId 
from Employee 
group by departmentId) 

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

# Write your MySQL query statement below
select d.name as Department,
e1.name as Employee,    
       e1.salary as Salary
from Employee as e1,Department as d
where e1.departmentId=d.id 
and 3>(
    select count(distinct e2.salary) 
    from Employee as e2 
    where e1.salary < e2.salary 
    and e1.departmentId=e2.departmentId
)

10.删除重复的电子邮箱

# Write your MySQL query statement below
delete p1 from Person p1,Person p2 
where p1.email = p2.email and p1.id>p2.id

11.上升的温度

# Write your MySQL query statement below
select a.id 
from Weather as a join Weather as b
where a.Temperature > b.Temperature
and dateDiff(a.recordDate , b.recordDate)=1

12.游戏玩法分析 I

# Write your MySQL query statement below
select player_id, min(event_date) first_login
from Activity
group by player_id

13.至少有5名直接下属的经理

select name 
from Employee as t1 join
(select managerId 
from Employee 
group by managerId 
having count(managerId) >= 5 ) as t2
on t1.id = t2.managerId

14.大的国家

# Write your MySQL query statement below
select name,population,area
from World
where population>=25000000
or area>=3000000

15.销售员

select name 
from SalesPerson 
where sales_id 
not in (select sales_id 
from Orders o 
left join Company c 
on o.com_id=c.com_id
where name="RED")

16.使用唯一标识码替换员工ID


select unique_id,name 
from Employees e 
left join EmployeeUNI u 
on e.id = u.id

17.排名靠前的旅行者

# Write your MySQL query statement below
select u.name, ifnull (sum(distance),0) as travelled_distance
from Users u left join Rides r on u.id = r.user_id
group by r.user_id 
order by travelled_distance desc,name asc

18.按日期分组销售产品

select sell_date,count(distinct product) as num_sold,
group_concat(distinct product order by product separator ",") products
from Activities
group by sell_date
order by sell_date

19.查找拥有有效邮箱的用户】

# Write your MySQL query statement below
select * 
from Users
where mail regexp "^[A-Za-z][A-Za-z0-9_.-]*@leetcode[.]com$"

20.患某种疾病的患者

# Write your MySQL query statement below
select * from Patients
where conditions
regexp "^DIAB1|\\sDIAB1"

21.进店却未进行过交易的顾客

# Write your MySQL query statement below
select customer_id, count(customer_id) count_no_trans
from Visits v left join Transactions t 
on v.visit_id = t.visit_id
where t.transaction_id is null
group by customer_id

22.银行账户概要II

# Write your MySQL query statement below
select name,sum(amount) balance
from Users u,Transactions t
where u.account = t.account 
group by u.account
having balance>10000

23.即使食物配送 I

# Write your MySQL query statement below
select ROUND(
  100 * AVG(order_date = customer_pref_delivery_date),2
) as immediate_percentage
from Delivery

24.丢失信息的雇员

select employee_id from
(select employee_id from Employees 
union all
select employee_id from Salaries) as t
group by employee_id
having count(*)=1
order by employee_id asc;

25.查阅近30天活跃用户数

select activity_date as day, count(distinct user_id) as active_users
from Activity 
where activity_date between "2019-06-28" and "2019-07-27"
group by activity_date

26.文章浏览 I

select distinct author_id as id 
from Views
where author_id = viewer_id
order by id asc

27.市场分析 I

select user_id as buyer_id, join_date,
count(if(year(order_date)=2019,1,null)) as orders_in_2019
from Users u left join Orders o on u.user_id=o.buyer_id
group by user_id

28.上级经理已离职的公司员工

select e1.employee_id as employee_id
from Employees e1 left join Employees e2
on e1.manager_id = e2.employee_id
where e1.salary <30000 and
e1.manager_id is not null and
e2.employee_id is null
order by e1.employee_id asc

29.富有客户的数量

select count(distinct customer_id) as rich_count
from Store 
where amount > 500

30.超过经理收入的员工

select e1.name as Employee
from Employee e1, Employee e2
where e1.managerId = e2.Id
and e1.salary > e2.salary 

31.员工奖金

select e.name,b.bonus 
from Employee e
left join Bonus b on e.empId = b.empId
where bonus is null or bonus < 1000

32.超过5名学生的课

select class 
from Courses
group by class
having count(class)>=5

33.好友申请 II:谁有最多的好友

select t1.ids as id, count(*) as num from
(select requester_id as ids from RequestAccepted
union all 
select accepter_id as ids from RequestAccepted) as t1
group by t1.ids
order by count(*) desc
limit 1

34.只出现一次的最大数字

select ifnull(
    (select num
    from MyNumbers
    group by num
    having count(*)=1
    order by num desc limit 1),null
)num

35.变更性别

update Salary
set sex = case sex
when "m" then "f"
else "m"
end;

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值