【高频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:

有些字符需要转义。

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

相关推荐
斯晨的AI笔记10 小时前
rsync 部署避坑:脚本执行位被 git 模式位抹掉,巡检备份双静默失败的排查与根治
数据库
严谨的麻辣烫10 小时前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
这个DBA有点耶10 小时前
innodb_autoinc_lock_mode的三种模式:批量插入时自增锁的持有逻辑与调优
数据库·架构·代码规范
我命由我1234510 小时前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
原子延迟10 小时前
视频太大怎么压缩发微信?先查限制再压的3种方法(2026)
开发语言·图像处理·人工智能·计算机视觉·音视频
高旭的旭11 小时前
图解 Android 系统架构:从 App、Framework、HAL 到 Linux 驱动
android·linux·系统架构
FITA阿泽要努力11 小时前
Fork Agent:共享上下文的艺术
开发语言·python·agent
小蒜学长11 小时前
基于Spring Boot+Vue3的在线游戏技术指导师匹配系统的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·在线游戏技术指导
hunterandroid11 小时前
Navigation Component 多模块导航与深度链接实战
android
快乐非自愿11 小时前
低代码落地费控系统:从报销流程到业财闭环的实操方案
android·低代码·rxjava·ai低代码