当前位置: 首页 > article >正文

【SQL篇】面试篇之子查询

1303 求团队人数

在这里插入图片描述

# 写法1
# Write your MySQL query statement below
select employee_id, count(*) over(partition by team_id) as team_size
from Employee

# 写法2
# Write your MySQL query statement below
select employee_id, team_size
from Employee e
join (
    select team_id, count(*) as team_size
    from Employee
    group by team_id
) tmp
on e.team_id = tmp.team_id

总结

  • 考察子查询
  • 学会使用窗口函数

512 游戏玩法分析 II

在这里插入图片描述

# 写法1
# Write your MySQL query statement below
select player_id, device_id
from (
    select player_id, device_id, 
    rank() over(partition by player_id order by event_date) as rk
    from Activity 
) tmp
where tmp.rk = 1

# 写法2
# Write your MySQL query statement below
select player_id, device_id
from Activity
where (player_id, event_date) in (
    select player_id, min(event_date)
    from Activity
    group by player_id
);

184 部门工资最高的员工

在这里插入图片描述
在这里插入图片描述

# 写法1
select d.name as Department, e.name as Employee, e.salary as salary
from Employee e
join Department d
on e.departmentId = d.id
where (e.departmentId, salary) in (
    select departmentId, max(salary)
    from Employee
    group by departmentId
);

# 写法2 rank()
select d.name as Department, e.name as  Employee, salary
from (
    select *,
    rank() over(partition by departmentId order by salary desc) as rk
    from Employee
) e, Department d
where e.departmentId = d.id and e.rk = 1;

# 写法3 dense_rank()
select d.name as Department, e.name as  Employee, salary
from (
    select *,
    dense_rank() over(partition by departmentId order by salary desc) as rk
    from Employee
) e, Department d
where e.departmentId = d.id and e.rk = 1;

总结

  1. row_number, rank(), dense_rank()的区别
  • rank()排序相同时会重复,总数不变,即会出现1、1、3这样的排序结果;
  • dense_rank()排序相同时会重复,总数会减少,即会出现1、1、2这样的排序结果;
  • row_number()排序相同时不会重复,会根据顺序排序。
  1. 注意点
  • row_number函数得到的列别名可用于order by 排序,因为order by执行在select之后。
  • where, group by, having都不可引用该列,因为这些语句执行在select之前,此时函数尚未计算出值。

1549 每件商品的最新订单

  • 1549题
# 写法1
# Write your MySQL query statement below
select p.product_name, p.product_id, order_id, order_date
from Products p
join Orders o
on p.product_id = o.product_id
where (p.product_id, order_date) in (
    select product_id, max(order_date)
    from Orders
    group by product_id
)
order by product_name, product_id, order_id

# 写法2
# Write your MySQL query statement below
select p.product_name, p.product_id, tmp.order_id, tmp.order_date
from (
    select *, dense_rank() over(partition by product_id order by order_date desc) as rk
    from Orders
    ) tmp, Products p
where tmp.product_id = p.product_id and rk = 1
order by product_name, product_id, order_id

总结

  • 该题与顾客表无关,我们只需要订单表和商品表就可。
  • 注意dense_rank()和row_number()的区别,不要用错。

1532 最近的三笔订单

  • 1532题
# 写法1
# Write your MySQL query statement below
select c.name as customer_name, c.customer_id, o.order_id, o.order_date
from (
    select *, rank() over(partition by customer_id order by order_date desc) as rk
    from Orders
) o, Customers c
where o.customer_id = c.customer_id and rk <= 3
order by customer_name, customer_id, order_date desc;

1831 每天的最大交易

在这里插入图片描述

# 写法1
# Write your MySQL query statement below
select transaction_id
from Transactions
where (day(day), amount) in (
    select day(day) as t, max(amount) as mx
    from Transactions
    group by t
)
order by transaction_id;

# 写法2
select transaction_id
from (
    select transaction_id, rank() over(partition by day(day) order by amount desc) as rk
    from Transactions t
) tmp
where rk = 1
order by transaction_id;

参考

  1. row_number()等函数详解

http://www.kler.cn/a/16307.html

相关文章:

  • 3. Sharding-Jdbc核⼼流 程+多种分⽚策略
  • 单片机设计电流与温度监控python上位机监控平台设计
  • 「Mac玩转仓颉内测版12」PTA刷题篇3 - L1-003 个位数统计
  • HTTP常见的状态码有哪些,都代表什么意思
  • win11 新建一个批处理,双击查看本机的IP地址
  • MFC工控项目实例二十九主对话框调用子对话框设定参数值
  • 一文解决MySQL突击面试,关键知识点总结
  • 解除Word的编辑保护【简单版】
  • 智能网联汽车城市化的进程和思考
  • next(), nextLine(),nextInt()报错分析
  • UG NX二次开发(C++)-建模-修改NXObject或者Feature的颜色(一)
  • Java学习报培训班好还是自学好?
  • 微服务SpringCloud面试题27问
  • appium-app测试-环境搭建手机和adb设置
  • 四和能聚分析做直播带货的商家通常发布什么类型的短视频
  • 电脑的选择
  • 06.toRef 和 toRefs
  • iOS 多线程使用示例
  • JSP 的本质原理解析:“编写的时候是JSP,心里想解读的是 java 源码“
  • 什么是Java中的反射机制?
  • Syncfusion Essential Studio Enterprise 2023.1 Crack
  • AOSP编译问题
  • 【Bus】编写一个Demo虚拟的总线-设备-驱动模型
  • 电话号码的字母组合
  • 荔枝派Zero(全志V3S)基于QT实现在LCD显示图片
  • 【五一创作】Scratch资料袋