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
相关推荐
云里雾里!5 分钟前
LeetCode 744. 寻找比目标字母大的最小字母 | 从低效到最优的二分解法优化
算法·leetcode
_ziva_6 分钟前
MAC-SQL 多智能体协作框架解析
数据库·oracle
最贪吃的虎6 分钟前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua
代码不停12 分钟前
MySQL事务
android·数据库·mysql
山峰哥13 分钟前
数据库工程与SQL调优实战:从原理到案例的深度解析
java·数据库·sql·oracle·性能优化·编辑器
OpsEye14 分钟前
Redis 内存碎片的隐形消耗——如何用 memory purge 命令释放空间?
运维·网络·数据库·redis·缓存·内存·监控
Tisfy30 分钟前
LeetCode 0865.具有所有最深节点的最小子树:深度优先搜索(一次DFS + Python5行)
算法·leetcode·深度优先·dfs·题解
Q741_14734 分钟前
C++ 队列 宽度优先搜索 BFS 力扣 429. N 叉树的层序遍历 C++ 每日一题
c++·算法·leetcode·bfs·宽度优先
施嘉伟40 分钟前
一次典型的 SQL 性能问题排查:临时表导致的隐藏性能陷阱
数据库·sql
IT 乔峰1 小时前
分享一个负载均衡的NDB高可用集群架构+部署详细说明
数据库·架构·负载均衡