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

有些字符需要转义。

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

相关推荐
roman_日积跬步-终至千里11 分钟前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
秦少游在淮海32 分钟前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用
const54440 分钟前
cpp自学 day2(—>运算符)
开发语言·c++
心扬41 分钟前
python生成器
开发语言·python
明月醉窗台41 分钟前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
duwei_wang44 分钟前
[Android]-Admob配置过多导致的慢消息
android
阿蒙Amon1 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
虾球xz1 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
沉到海底去吧Go1 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
CodeWithMe1 小时前
【C/C++】namespace + macro混用场景
c语言·开发语言·c++