【高频SQL基础50题】46-50

SQL时刻。

目录

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

2.确认率

[3.游戏玩法分析 IV](#3.游戏玩法分析 IV)

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

5.查找拥有有效邮箱的用户

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

子查询。

1.先找出至少有5名直接下属的经理号managerId

2.根据经理号找到对应名字

sql 复制代码
# Write your MySQL query statement below
select name
from Employee
where id in (
    select managerId
    from Employee
    group by managerId
    having count(managerId)>=5
)

2.确认率

左连接+子查询。

sql 复制代码
# Write your MySQL query statement below
select s.user_id,round(count(if(c.action='confirmed',1,null))/count(*),2) as confirmation_rate
from Signups s left join Confirmations c
on s.user_id=c.user_id
group by s.user_id;

3.游戏玩法分析 IV

先过滤出每个用户的首次登陆日期,然后左关联,筛选次日存在的记录的比例。

sql 复制代码
# Write your MySQL query statement below
select round(avg(a.event_date is not null),2) fraction
from 
(
    select player_id,min(event_date) as login
    from activity
    group by player_id
) p
left join activity a
on p.player_id=a.player_id and datediff(a.event_date,p.login)=1

ps:

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

贴优秀题解。

sql 复制代码
# Write your MySQL query statement below
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.salary<e2.salary and e1.departmentId=e2.departmentId
)
order by Department.name,Salary desc;

5.查找拥有有效邮箱的用户

sql 复制代码
# Write your MySQL query statement below
#正则表达式,有意思
select t1.*
from Users t1
where t1.mail REGEXP '^[a-zA-Z][a-zA-Z0-9_\./\-]*\@leetcode[\.]com$';

ps:

有些字符需要转义。

一遍终于结束了!!!!!

相关推荐
一笑的小酒馆27 分钟前
AndroidKMP之网络请求
android
小灰灰搞电子2 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
SkyWalking中文站2 小时前
BanyanDB 0.11.0:新特性与升级指南
数据库
传奇开心果编程3 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马3 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob3 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
₍˄·͈༝·͈˄*₎◞ ̑̑码3 小时前
MyBatis操作数据库
数据库·mybatis
aqi004 小时前
鸿蒙版本的JSBridge兼容与安卓配套的H5啦
android·华为·harmonyos·鸿蒙·移动应用
淡海水4 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
白山编程大哥4 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python