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

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

在这里插入图片描述

一、查询

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

select
    customer_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. 计算特殊奖金

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

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

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

1112. 每位学生的最高成绩

select  student_id,course_id,grade 
from (
select
    e.*
    ,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. 组合两个表

select
    firstName,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. 排名靠前的旅行者


select
    name,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. 销售员

select
    name
from SalesPerson
where sales_id not in (
    select
        sales_id
    from Orders o
    left join Company c on o.com_id = c.com_id
    where 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 value
	
from Expressions e 
join Variables v1 on e.left_operand = v1.name
join Variables v2 on e.right_operand = v2.name 

1212. 查询球队积分

with t1 as (
	select
		m.*,
		case when host_goals > guest_goals then 3
			when host_goals = guest_goals then 1
			else 0 
		end as host_p
		,case when host_goals < guest_goals then 3
			when host_goals = guest_goals then 1
			else 0 
		end as gust_p
	from Matches m 
)

select
	t.team_id ,team_name,nvl(sum_p,0) as num_points
from Teams t 
left join (
	select
		team_id,sum(p) as sum_p
	from (
	select
		host_team as team_id
		,host_p as p 
	from t1
	union all 
	select
		guest_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 Logins
    where to_char(time_stamp,'yyyy') = '2020'
) where rk = 1

511. 游戏玩法分析 I

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

1571. 仓库经理

select
    name 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 (
    select
        customer_number
    from Orders
    group by customer_number
    order by count(order_number) desc
) where rownum <= 1

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

select
    to_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


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

1445. 苹果和桔子


select
    to_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 (
    select
        from_id as p1
        ,to_id as p2
        ,duration
    from Calls
    union all
    select
        to_id as p1
        ,from_id as p2
        ,duration
    from Calls
)

select
    p1 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


select
    u.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. 查找重复的电子邮箱

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

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

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

1511. 消费者下单频率


select distinct customer_id,name from (
    select
        c.customer_id,c.name
        , count(to_char(order_date,'yyyy-mm') ) over(partition by c.customer_id) as mon
    from Orders o 
    left join Product p on o.product_id = p.product_id
    left join Customers c on o.customer_id = c.customer_id
    where 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. 上月播放的儿童适宜电影

select
    distinct 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 Calls
    union all 
    select callee_id as c1,caller_id as c2 ,duration from Calls
)

select name as country from (
    select
        c.name
        ,avg(duration) as avg_duration
    from t1  
    join Person p on p.id = t1.c1 
    join Country c on substr(p.phone_number,1,3) = c.country_code
    group 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 cnt
    from (
    select
        seat_id
        ,seat_id - rank() over(order by seat_id) as gap
    from Cinema
    where free = 1
    )
) where cnt > 1
order by seat_id

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


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

613. 直线上的最近距离


select
    min( abs(x - lag_x) ) as shortest
from (
select
    x,lag(x) over(order by x) as lag_x
from Point
)

1965. 丢失信息的雇员


select
    nvl(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 
)

select
    distinct 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. 树节点

select
    id
    ,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

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

1783. 大满贯数量

with t1 as (
    select
        *
    from Championships
    unpivot(
        cp for game in (
                Wimbledon as 'Wimbledon'
                ,Fr_open as 'Fr_open'
                ,US_open as 'US_open'
                ,Au_open as 'Au_open'
            )
    )
) 

select
    p.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 账户

select
    distinct 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 (
select
    player_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 (
select
    e.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 (
    select
        product_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. 最近的三笔订单

select
    customer_name
    ,customer_id
    ,order_id
    ,order_date
from (
    select
        name 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. 每天的最大交易

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

六、子查询

1350. 院系无效的学生

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

1303. 求团队人数

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

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

1077. 项目员工 III

select 
    project_id,employee_id
from (
    select
        project_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. 找到连续区间的开始和结束数字

select
    min(log_id) as start_id
    ,max(log_id) as end_id
from(
    select
        log_id
        ,log_id - row_number() over(order by log_id) as gap
    from Logs l 
)group by gap
order by start_id

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

select 
    a.customer_id,a.product_id,p.product_name
from (
    select
        product_id,customer_id
        ,rank() over(partition by customer_id order by count(order_id) desc) as rk 
    from Orders
    group 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 (
    select
        user_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 gap
    from 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.mfbz.cn/a/443369.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Windows-Wireguard-Watchdog,Wireguard服务端DDNS客户端自动重连

Windows-Wireguard-Watchdog 背景&#xff1a;Wireguard服务端是动态IP&#xff0c;Wireguard客户端配置的服务端地址是域名 Endpoint xx.xxx.com:51820&#xff0c;写的DDNS脚本检测到IP变更会自动更新域名解析。每次宽带重拨IP就会变更&#xff0c;由于客户端重连使用的不是…

VSCODE解决git合并过程中的冲突问题;error: failed to push some refs to

1&#xff1a;异常现象 推送有冲突的git修改到远端的时候&#xff0c;会有如下提示 git.exe push --progress “origin” master:master To http://gitlab.xxx.com/dujunqiu/test.git ! [rejected] master -> master (fetch first) error: failed to push some refs to ‘…

00在linux环境下搭建stm32开发环境

文章目录 前言一、环境搭建1.arm-none-eabi-gcc2.openocd 三、创建stm32标准库工程1.创建工程目录2.修改stm32_flash.ld文件3.写makefile文件4.修改core_cm3.c5.写main函数并下载到板子上 最后 前言 我在那天终于说服自己将系统换成了linux系统了&#xff0c;当换成了linux系统…

博士推荐 | 薄膜、涂层技术和液晶材料/器件领域的博士

编辑 / 木子 审核 / 朝阳 伟骅英才 伟骅英才致力于以大数据、区块链、AI人工智能等前沿技术打造开放的人力资本生态&#xff0c;用科技解决职业领域问题&#xff0c;提升行业数字化服务水平&#xff0c;提供创新型的产业与人才一体化服务的人力资源解决方案和示范平台&#x…

tomcat通过service.bat install方式安装,内存不够了怎么办?

1.通过service.bat安装 安装命令再tomcat的bin目录下&#xff0c;执行命令 .\service.bat install Apache Tomcat 8.5 Tomcat8之后就会在服务里面有个tomcat服务 2. 如何增加tomcat内存呢&#xff1f; 通过service.bat安装肯定再service.bat中配置啊。 再service.bat文件中…

蓝桥杯python常用内置函数

一、 abs() #返回数字的绝对值 例&#xff1a; 二、 all() #判断给定的可迭代参数中的所有元素是否都为True&#xff0c;若是则返回True&#xff0c;反之返回False 例&#xff1a; 三、 any() #判断给定的可迭代参数是否都为False&#xff0c;全为False则返回False&am…

论文的引用书写方法

前置操作 1、全选文献 2、在开始选项卡 段落功能区 选择编号功能 3、设置编号格式 [1] 论文的引用 1、光标放在需要引用论文的地方 2、选择引用选项卡 点击交叉引用 3、引用类型为编号项 引用内容为段落编号 选择需要的第几条参考文献

应对高并发的软件架构之道

在去年年终总结的时候&#xff0c;我提出了这样的困惑&#xff0c;究竟什么是真正的技术能力&#xff0c;是对于各种底层技术的钻研吗&#xff1f;钻研是好事&#xff0c;但实践下来&#xff0c;深入钻研并不在实际工作中有用&#xff0c;且钻研的越深&#xff0c;忘得越快&…

Android开发中View绘制流程、局限性及优化方案分析

一、引言 在Android开发中&#xff0c;View的绘制流程是一个核心概念。了解和掌握View的绘制流程&#xff0c;可以帮助我们编写出更高效、更流畅的UI组件和自定义View。然而&#xff0c;View的绘制流程也存在一定的局限性&#xff0c;如果不加以优化&#xff0c;可能会导致应用…

美食网页成品 HTML美食网页设计制作 前端美食网页开发 热门美食特产网页制作中国传统特色小吃-臭豆腐 6页面 美食主题 HTML5 带设计说明

美食网页成品 HTML美食网页设计制作 前端美食网页开发 热门美食特产网页制作 中国传统特色小吃-臭豆腐 6页面 美食主题 HTML5 带设计说明 http://www.yuanle.net.cn/anli/30/4138.html

【AI视野·今日NLP 自然语言处理论文速览 第八十四期】Thu, 7 Mar 2024

AI视野今日CS.NLP 自然语言处理论文速览 Thu, 7 Mar 2024 Totally 52 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Computation and Language Papers The Heuristic Core: Understanding Subnetwork Generalization in Pretrained Language Models Authors Adith…

一招鲜吃遍天之Haproxy集群

四层&#xff1a; LVS&#xff1a;Linux Virtual Server Nginx&#xff1a; HAProxy&#xff1a;High Availability Proxy 七层: HAProxy Nginx 硬件&#xff1a; F5 F5 | 多云安全和应用交付 Netscaler NetScaler: Application Delivery at Scale Array 北京华耀科技…

最新:Selenium操作已经打开的Chrome(免登录)

最近重新尝试了一下&#xff0c;之前写的博客内容。重新捋了一下思路。 目的就是&#xff0c;selenium在需要登录的网站面前&#xff0c;可能就显得有些乏力&#xff0c;因此是不是有一种东西&#xff0c;可以操作它打开我们之前打开过的网站&#xff0c;这样就不用登录了。 …

北漂程序员整理阿里云服务器地域机房所在城市表

2024年最新阿里云服务器地域分布表&#xff0c;地域指数据中心所在的地理区域&#xff0c;通常按照数据中心所在的城市划分&#xff0c;例如华北2&#xff08;北京&#xff09;地域表示数据中心所在的城市是北京。阿里云地域分为四部分即中国、亚太其他国家、欧洲与美洲和中东&…

创建数据表

Oracle从入门到总裁:https://blog.csdn.net/weixin_67859959/article/details/135209645 如果要进行数据表的创建 create table 表名称 (列名称 类型 [DEFAULT 默认值 ] ,列名称 类型 [DEFAULT 默认值 ] ,列名称 类型 [DEFAULT 默认值 ] ,...列名称 类型 [DEFAULT 默认值 ] )…

鸿蒙应用开发学习:使用视频播放(Video)组件播放视频和音频文件

一、前言 播放音视频是手机的重要功能之一&#xff0c;近期我学习了在鸿蒙系统应用开发中实现音视频的播放功能&#xff0c;应用中使用到了视频播放(Video)组件&#xff0c;ohos.file.picker&#xff08;选择器&#xff09;。特撰此文分享一下我的学习经历。 二、参考资料 本…

详解Linux例行性工作

例行性工作&#xff08;计划任务&#xff09; 场景&#xff1a; 生活中&#xff0c;我们有太多场景需要使用到闹钟&#xff0c;比如早上7点起床&#xff0c;下午4点开会&#xff0c;晚上8点购物&#xff0c;等等。再Linux系统里&#xff0c;我们同样也有类似的需求。比如我们…

创新企业成长模型:嘉绩咨询深化招商教育与系统策划

在当今企业发展与市场拓展的竞争激烈背景下&#xff0c;嘉绩咨询已凸显其在招商体系孵化领域的领先地位。集团不仅在招商教育、招商落地支持、陪跑孵化及渠道商学院搭建等业务领域提供全面的服务&#xff0c;同时构筑了与众不同的企业成长循环模型。 嘉绩咨询秉承其"教育策…

四、DMSP/OLS等夜间灯光数据贫困地区识别——相对误差相关折线图制作

一、前言 前文对于MPI和灯光指数拟合、误差分析&#xff0c;本文重点介绍地理加权分析&#xff0c;但是在此之前给大家介绍一下专业表格制作&#xff0c;其实专业的软件有很多像Orgin、棱镜等&#xff0c;到我们熟知的Excel&#xff0c;其实各有千秋&#xff0c;Excel入手容易…

软件杯 交通目标检测-行人车辆检测流量计数 - 软件杯

文章目录 0 前言1\. 目标检测概况1.1 什么是目标检测&#xff1f;1.2 发展阶段 2\. 行人检测2.1 行人检测简介2.2 行人检测技术难点2.3 行人检测实现效果2.4 关键代码-训练过程 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 毕业设计…
最新文章