LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 上

题目:1731. 每位经理的下属员工数量




题解:

sql 复制代码
select employee_id,name,reports_count,average_age
from Employees t1,(
    select reports_to,count(*) reports_count,round(avg(age)) average_age
    from Employees
    where reports_to is not null
    group by reports_to
) t2
where t1.employee_id=t2.reports_to
order by employee_id asc

题目:1789. 员工的直属部门



题解:

sql 复制代码
select employee_id,department_id from Employee
group by employee_id
having count(department_id)=1

union 

select employee_id,department_id from Employee
where primary_flag='Y'
sql 复制代码
select employee_id,department_id from Employee
where primary_flag='Y' or employee_id in (
    select employee_id from Employee
    group by employee_id
    having count(department_id)=1
)

题目:610. 判断三角形



题解:

sql 复制代码
select x,y,z,
if(x+y>z and x+z>y and y+z>x,'Yes','No') triangle
from Triangle
sql 复制代码
select x, y, z,
    case
        when x+y>z and x+z>y and y+z>x then 'Yes'
        else 'No'
    end as triangle
from Triangle

题目:180. 连续出现的数字



题解:

sql 复制代码
select distinct l1.num ConsecutiveNums from 
Logs l1,
Logs l2,
Logs l3
where l1.id=l2.id+1 and l2.id=l3.id+1
and l1.num=l2.num and l2.num=l3.num
相关推荐
cspttty15 分钟前
需求预测校招入门:SQL、Excel、统计模型和业务指标怎么学
大数据·数据库
2501_9336707918 分钟前
面向物流数据分析校招:SQL、Excel、BI、WMS/TMS该怎么准备
数据库
Code_流苏19 分钟前
AI 知识库和数据库有什么区别?从“查订单”到“问规则”讲清楚
数据库·ai·agent·知识库
YangYang9YangYan23 分钟前
财务 BP 校招面试案例整理|业务场景题 + 数据案例,2027 届求职
大数据·数据库
圣保罗的大教堂32 分钟前
leetcode 1665. 完成所有任务的最少初始能量 中等
leetcode
隔窗听雨眠38 分钟前
KingbaseES性能优化实战指南:从操作系统调优到SQL等价改写的完整方法论
数据库·kingbasees
NineData1 小时前
NineData 与统信服务器操作系统完成兼容认证
数据库·oracle·操作系统·软件·数据库管理工具·ninedata·统信
十二同学啊11 小时前
向量数据库:从核心原理到 RAG 与 Java 实战
数据库
蓝速科技12 小时前
会议室门牌公告通知发布选型与落地指南丨蓝速科技
大数据·运维·数据库·人工智能·科技
一 乐14 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序