Oracle LeetCode 高频 SQL 50 题(进阶版)

https://leetcode.cn/studyplan/sql-premium-50/

在这里插入图片描述

一、查询

1821. 寻找今年具有正收入的客户

selectcustomer_id
from Customers
where year = 2021
group by customer_id
having sum(revenue) > 0

183. 从不订购的客户

select c.name as Customers
from Customers c
left join Orders o on c.id = o.customerId
where o.id is null

1873. 计算特殊奖金

selectemployee_id,case when mod(employee_id,2) = 1 and substr(name,1,1) != 'M' then salaryelse 0end as bonus
from Employees
order by employee_id

1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客

selectcustomer_id,customer_name
from Customers
where customer_id in (selectcustomer_idfrom Orderswhere product_name in ('A','B')and customer_id not in (select customer_id from Orders where product_name = 'C')group by customer_idhaving count(distinct product_name) = 2
)
order by customer_id

1112. 每位学生的最高成绩

select  student_id,course_id,grade 
from (
selecte.*,rank() over(partition by student_id order by grade desc,course_id) as rk 
from Enrollments e 
) where rk = 1
order by student_id

二、连接

175. 组合两个表

selectfirstName,lastName,city,state
from Person p 
left join Address a on p.PersonId = a.PersonId

1607. 没有卖出的卖家


select seller_name
from Seller
where seller_id not in (
select seller_id
from Orders
where to_char(sale_date,'yyyy') = '2020'
)order by seller_name

1407. 排名靠前的旅行者


selectname,nvl( sum(distance) ,0) as travelled_distance
from Users u 
left join Rides r on u.id = r.user_id
group by name,u.id
order by travelled_distance desc,name 

607. 销售员

selectname
from SalesPerson
where sales_id not in (selectsales_idfrom Orders oleft join Company c on o.com_id = c.com_idwhere c.name = 'RED'
)

1440. 计算布尔表达式的值

select e.*,case when operator = '>' and v1.value > v2.value then 'true'when operator = '<' and v1.value < v2.value then 'true'when operator = '=' and v1.value = v2.value then 'true'else 'false'end as valuefrom Expressions e 
join Variables v1 on e.left_operand = v1.name
join Variables v2 on e.right_operand = v2.name 

1212. 查询球队积分

with t1 as (selectm.*,case when host_goals > guest_goals then 3when host_goals = guest_goals then 1else 0 end as host_p,case when host_goals < guest_goals then 3when host_goals = guest_goals then 1else 0 end as gust_pfrom Matches m 
)selectt.team_id ,team_name,nvl(sum_p,0) as num_points
from Teams t 
left join (selectteam_id,sum(p) as sum_pfrom (selecthost_team as team_id,host_p as p from t1union all selectguest_team as team_id,gust_p as p from t1) group by team_id
) a on t.team_id = a.team_id
order by num_points desc,team_id

三、聚合函数

1890. 2020年最后一次登录

select user_id,last_stamp 
from (select user_id,time_stamp as last_stamp ,row_number() over(partition by user_id order by time_stamp desc) as rk from Loginswhere to_char(time_stamp,'yyyy') = '2020'
) where rk = 1

511. 游戏玩法分析 I

selectplayer_id,to_char( min(event_date) ,'yyyy-mm-dd')  as  first_login
from Activity
group by player_id

1571. 仓库经理

selectname as warehouse_name,nvl(sum(units * Width * Length * Height),0) as volume
from Warehouse w 
left join Products p on w.product_id = p.product_id
group by name

586. 订单最多的客户

select * from (selectcustomer_numberfrom Ordersgroup by customer_numberorder by count(order_number) desc
) where rownum <= 1

1741. 查找每个员工花费的总时间

selectto_char(event_day,'yyyy-mm-dd') as day,emp_id,sum( out_time - in_time ) as total_time
from Employees
group by emp_id,event_day

1173. 即时食物配送 I


selectround(count(case when order_date = customer_pref_delivery_date then delivery_id end)*100/ count( delivery_id ),2) as immediate_percentage
from Delivery

1445. 苹果和桔子


selectto_char(sale_date) as sale_date,sum(case when fruit = 'apples' then sold_num end) - sum(case when fruit = 'oranges' then sold_num end)as diff
from Sales
group by to_char(sale_date)
order by sale_date

1699. 两人之间的通话次数

with t1 as (selectfrom_id as p1,to_id as p2,durationfrom Callsunion allselectto_id as p1,from_id as p2,durationfrom Calls
)selectp1 as person1,p2 as person2,count(*) as call_count,sum(duration) as total_duration
from t1 
where p1 < p2 
group by p1,p2 

四、排序和分组

1587. 银行账户概要 II


selectu.name,sum(amount) as balance
from Users u 
left join Transactions t on u.account = t.account
group by u.account,u.name
having sum(amount) > 10000

182. 查找重复的电子邮箱

selectemail
from Person
group by email
having count(id) > 1

1050. 合作过至少三次的演员和导演

selectactor_id,director_id
from ActorDirector
group by actor_id,director_id
having count(*) >= 3

1511. 消费者下单频率


select distinct customer_id,name from (selectc.customer_id,c.name, count(to_char(order_date,'yyyy-mm') ) over(partition by c.customer_id) as monfrom Orders o left join Product p on o.product_id = p.product_idleft join Customers c on o.customer_id = c.customer_idwhere to_char(order_date,'yyyy-mm') in ('2020-06','2020-07')group by c.customer_id,c.name,to_char(order_date,'yyyy-mm')having sum(price * quantity) >= 100
) where mon > 1

1495. 上月播放的儿童适宜电影

selectdistinct title 
from TVProgram t 
join Content c on t.content_id = c.content_id
where Kids_content = 'Y'
and to_char(program_date,'yyyy-mm') = '2020-06'
and content_type = 'Movies'

1501. 可以放心投资的国家

with t1 as (select caller_id as c1,callee_id as c2 ,duration from Callsunion all select callee_id as c1,caller_id as c2 ,duration from Calls
)select name as country from (selectc.name,avg(duration) as avg_durationfrom t1  join Person p on p.id = t1.c1 join Country c on substr(p.phone_number,1,3) = c.country_codegroup by c.name
) where avg_duration > (select avg(duration) from Calls)

五、高级查询和连接

603. 连续空余座位


select seat_id from (select seat_id,count(seat_id) over(partition by gap) as cntfrom (selectseat_id,seat_id - rank() over(order by seat_id) as gapfrom Cinemawhere free = 1)
) where cnt > 1
order by seat_id

1795. 每个产品在不同商店的价格


selectproduct_id , store  , price 
from Products
unpivot(price for store in (store1 as 'store1',store2 as 'store2',store3 as 'store3')
)

613. 直线上的最近距离


selectmin( abs(x - lag_x) ) as shortest
from (
selectx,lag(x) over(order by x) as lag_x
from Point
)

1965. 丢失信息的雇员


selectnvl(e.employee_id,s.employee_id) as employee_id
from Employees e 
full join Salaries  s on e.employee_id = s.employee_id
where e.employee_id is null or s.employee_id is null
order by employee_id

1264. 页面推荐

with t1 as (select user1_id as u1 ,user2_id as u2 from Friendship  union all select user2_id as u1 ,user1_id as u2 from Friendship 
)selectdistinct page_id as recommended_page
from Likes l
join t1  on l.user_id = t1.u2
and t1.u1 = 1
and page_id not in (select page_id from Likes where user_id = 1)

608. 树节点

selectid,case when p_id is null then 'Root'when id not in (select p_id from tree where p_id is not null) then 'Leaf'else 'Inner'end as type
from Tree

534. 游戏玩法分析 III

selectplayer_id,to_char(event_date,'yyyy-mm-dd') as event_date,sum(games_played) over(partition by player_id order by event_daterows between unbounded preceding and current row ) as games_played_so_far
from Activity

1783. 大满贯数量

with t1 as (select*from Championshipsunpivot(cp for game in (Wimbledon as 'Wimbledon',Fr_open as 'Fr_open',US_open as 'US_open',Au_open as 'Au_open'))
) selectp.player_id,player_name,count(*) as grand_slams_count
from t1 
join Players p on p.player_id = t1.cp 
group by  p.player_id,player_name

1747. 应该被禁止的 Leetflex 账户

selectdistinct l1.account_id
from LogInfo l1 join LogInfo l2 on l1.account_id = l2.account_id
and l1.ip_address != l2.ip_address
and l1.login between l2.login and l2.logout

512. 游戏玩法分析 II

select     player_id,device_id
from (
selectplayer_id,device_id,rank() over(partition by player_id order by  event_date) as rk 
from Activity
) where rk =1 

184. 部门工资最高的员工

select Department,Employee,Salary
from (
selecte.name as EMPLOYEE,e.salary,d.name as Department,rank() over(partition by d.name order by salary desc) as rk 
from Employee e 
join Department d on e.departmentId = d.id
) where rk = 1

1549. 每件商品的最新订单


select     product_name,product_id,order_id,order_date
from (selectproduct_name,o.product_id,order_id,to_char(order_date,'yyyy-mm-dd') as order_date,rank() over (partition by o.product_id order by order_date desc) as rk from Orders o join Products p on o.product_id = p.product_id
) where rk = 1
order by product_name,product_id,order_id

1532. 最近的三笔订单

selectcustomer_name,customer_id,order_id,order_date
from (selectname as customer_name,o.customer_id,order_id,to_char(order_date,'yyyy-mm-dd') as order_date,row_number() over(partition by o.customer_id order by order_date desc) as rk from Orders o join Customers c on o.customer_id = c.customer_id
)where rk <= 3
order by customer_name,customer_id,order_date desc

1831. 每天的最大交易

selecttransaction_id
from (selecttransaction_id,rank() over(partition by trunc(day) order by amount desc ) as rk from Transactions
) where rk = 1
order by transaction_id

六、子查询

1350. 院系无效的学生

selectid,name
from Students
where department_id not in (select id from Departments
)

1303. 求团队人数

selectemployee_id,count(employee_id) over(partition by team_id) as team_size
from Employee

七、窗口函数和公共表表达式CTE

1077. 项目员工 III

select project_id,employee_id
from (selectproject_id,p.employee_id,rank() over(partition by project_id order by experience_years desc) as rk from Project p left join Employee e  on e.employee_id = p.employee_id
)where rk = 1

1285. 找到连续区间的开始和结束数字

selectmin(log_id) as start_id,max(log_id) as end_id
from(selectlog_id,log_id - row_number() over(order by log_id) as gapfrom Logs l 
)group by gap
order by start_id

1596. 每位顾客最经常订购的商品

select a.customer_id,a.product_id,p.product_name
from (selectproduct_id,customer_id,rank() over(partition by customer_id order by count(order_id) desc) as rk from Ordersgroup by product_id,customer_id
) a 
join Products p on a.product_id = p.product_id and a.rk = 1

1709. 访问日期之间最大的空档期

select user_id,max(gap) as biggest_window
from (selectuser_id,visit_date,lead(visit_date,1,to_date('2021-1-1','yyyy-mm-dd')) over(partition by user_id order by visit_date) - visit_date as gapfrom UserVisits
)group by user_id 

1270. 向公司 CEO 汇报工作的所有人


select distinct employee_id
from Employees
where employee_id != 1
start with manager_id = 1
connect by nocycle prior employee_id = manager_id

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/732607.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【蓝牙协议栈】【经典蓝牙】【BLE蓝牙】蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM)

目录 1. 蓝牙协议规范&#xff08;HCI、L2CAP、SDP、RFOCMM&#xff09; 1.1 主机控制接口协议 HCI 1.2 逻辑链路控制与适配协议 L2CAP 1.3 服务发现协议SDP 1.4 串口仿真协议 RFCOMM 1. 蓝牙协议规范&#xff08;HCI、L2CAP、SDP、RFOCMM&#xff09; 1.1 主机控制接口协…

七、软考-系统架构设计师笔记-数据库设计基础知识

1、数据库基础概念 数据库基本概念 数据(Data)数据库(Database)数据库管理系统(DBMS)数据库系统(DBS) 1.数据(Data) 是数据库中存储的基本对象&#xff0c;是描述事物的符号记录。 数据的种类&#xff1a; 文本、图形、图像、音频、视频等。 2.数据库(Database, DB) 数据库…

基于机器学习的网络入侵检测与特征选择及随机森林分类器性能评估(NSL-KDD数据集)

简介 本文将详细介绍如何利用Python和相关机器学习库对NSL-KDD数据集进行预处理&#xff0c;特征选择&#xff0c;并通过随机森林算法构建网络入侵检测模型。同时&#xff0c;还将展示如何计算并可视化模型的ROC曲线以评估其性能。 首先&#xff0c;我们导入了必要的库&#…

Unity 让角色动起来(动画控制器)

下载素材&#xff1a; 导入后&#xff0c;找到预制体和动画。 新建动画控制器&#xff0c;拖动到预制体的新版动画组件上。 建立动画关系 创建脚本&#xff0c;挂载到预制体上。 using System.Collections; using System.Collections.Generic; using UnityEngine;public c…

Swift SwiftUI 学习笔记 2024

Swift SwiftUI 学习笔记 2024 一、资源 视频资源 StanfordUnivercity 公开课 2023: https://cs193p.sites.stanford.edu/2023 教程 Swift 初识&#xff1a;基础语法&#xff1a;https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/…

工地安全反光衣穿戴监测报警摄像机

工地安全反光衣穿戴监测报警摄像机是为了提高工地施工人员的安全意识和监管效率而设计的。这种设备结合了反光衣、监测系统和报警摄像机的功能&#xff0c;可以有效减少工地事故的发生。 首先&#xff0c;工地安全反光衣是一种具有高度可见度的服装&#xff0c;能够使穿戴者在夜…

程序如何知道mqtt设备是否在线

在做物联网设备的时候经常会碰到设备的在线与掉线 问题&#xff1a;emqx如何来实现这个在线与掉线 实现&#xff1a;添加一个规则&#xff0c;程序监控这个规则 1、SELECT * FROM "$events/client_connected", "$events/client_disconnected" 2、添加一…

离散数学——(4)

目录 1.主析取范式 2.大项 3.主合区范式 4.范式的求法 真值表法 5.推理理论 直接证法 1.主析取范式 2.大项 3.主合区范式 4.范式的求法 真值表法 5.推理理论 直接证法

【C++】手把手教你模拟实现 vector

目录 一、构造/析构/拷贝 1、构造函数 1️⃣无参的构造函数 2️⃣带参的构造函数 3️⃣类模板的构造函数 2、析构函数 3、拷贝构造 二、修改操作 1、reserve 【错误版本】 &#x1f31f;【解答】正确版本 2、resize 3、push_back 4、pop_back 5、insert 6、era…

pytorch(六、七)多维特征数据的输入、加载数据集的类

文章目录 多维特征数据的输入代码 加载数据集概念np.loadtxt()读取数据dataloadertorchvision获取数据集代码 三种梯度下降批量梯度下降BGD随机梯度下降SGD小批量随机梯度下降MBGD代码 多维特征数据的输入 对于一个多维数据&#xff0c;其行表示一个样本&#xff0c;列表示样本…

基于Java的社区买菜系统(Vue.js+SpringBoot)

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、系统设计2.1 功能模块设计2.1.1 数据中心模块2.1.2 菜品分类模块2.1.3 菜品档案模块2.1.4 菜品订单模块2.1.5 菜品收藏模块2.1.6 收货地址模块 2.2 可行性分析2.3 用例分析2.4 实体类设计2.4.1 菜品分类模块2.4.2 菜品档案模块2.4.3…

Linux内存映射

目录 背景 一、什么是内存映射&#xff1f; 二、mman函数 1.权限问题 2.总线错误 3.内存权限 4.读文件内容 5.映射与文件 6.非法参数错误 7.偏移量大小 8.映射内存大小 8.1 申请6k,访问5k 8.2 申请2k&#xff0c;访问3k 8.3 返回值检查 三、内存映射实现 四…

SSH移植到BusyBox

手动编译SSH安装挺麻烦的&#xff0c;本文主要是我大量借鉴和实践总结出来的流程&#xff0c;一步一按照做不会有太大问题。 移植平台&#xff1a;IMX6UL(迅为开发板) 根文件系统&#xff1a;BusyBox 所有操作都建议不要在root账户下运行&#xff0c;并且make install的安装路…

【Python】专栏文章索引

为了方便 快速定位 和 便于文章间的相互引用等 作为一个快速准确的导航工具 Python 目录&#xff1a; &#xff08;一&#xff09;装饰器函数 &#xff08;二&#xff09;牛客网—软件开发-Python专项练习 &#xff08;三&#xff09;装饰器函数 &#xff08;四&#xff0…

【深度学习】自动求导中有时为什么要先sum()再backward()

创作日志&#xff1a; 在看李沐学深度学习&#xff0c;“深度学习中&#xff0c;我们的目的不是计算微分矩阵&#xff0c;而是批量中每个样本单独计算的偏导数之和”&#xff0c;对这句话一知半解&#xff0c;自己动手推导一下。 一、理解 在深度学习中&#xff0c;被求导的对…

CSS的盒子模型:掌握网页设计的基石!

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

Linux的进程调度实现

经常被问到进程的调度算法有哪些&#xff0c;什么先进先出、短进程优先、时间片轮转、多级反馈多列等等算法能说一大堆&#xff1f;那具体的&#xff0c;linux内核使用了什么样的算法&#xff0c;且来探究一下。 本文所引用源码基于linux内核2.6.34版本。 目录 调度器类 从 s…

探索 PostgreSQL 的高级数据类型 - 第 1 部分

数组和枚举 PostgreSQL 因其可扩展性和多功能性而备受欢迎&#xff0c;除了传统的整数和字符串之外&#xff0c;它还提供了多种数据类型。其中&#xff0c;包括数组和枚举&#xff0c;其为开发者提供了高级的数据建模能力。本文中&#xff0c;我们将深入研究这些复杂的数据类型…

Unity中PICO实现 隔空取物 和 接触抓取物体

文章目录 前言一、隔空取物1、XR Grab Interactable2、调节扔出去时的相关系数3、用手柄射线指向需要抓取的物体后&#xff0c;按下侧边扳机键即可抓取 二、接触抓取物体1、替换手柄上抓取物体的脚本2、在手柄上添加 接触抓取物体的脚本3、在手柄上添加碰撞盒触发器4、在需要抓…

PHAMB: 病毒数据分箱

Genome binning of viral entities from bulk metagenomics data | Nature Communications 安装 ### New dependencies *Recommended* conda install -c conda-forge mamba mamba create -n phamb python3.9 conda activate phamb mamba install -c conda-forge -c biocond…