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

有些字符需要转义。

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

相关推荐
马克Markorg7 小时前
常见的向量数据库和具有向量数据库能力的数据库
数据库
冷雨夜中漫步7 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴7 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再7 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
2501_916008898 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
m0_736919109 小时前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934739 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
玉梅小洋9 小时前
Windows 10 Android 构建配置指南
android·windows
helloworldandy9 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python