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
相关推荐
怪兽学LLM6 分钟前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展
Csvn8 分钟前
📊 SQL 入门 Day 2:排序与去重 — 让数据有序呈现
sql
曹牧22 分钟前
Oracle:快速定位最新数据
数据库·oracle
Zhu7581 小时前
在Docker环境部署ApacheGuacamole,对接PG数据库
数据库·docker·容器
退休倒计时1 小时前
【每日一题】LeetCode 39. 组合总和 TypeScript
算法·leetcode·typescript
Leighteen1 小时前
Redis与MySQL双写一致性:从问题根源到工程落地
数据库·redis·mysql
AI-好学者2 小时前
阶段二-Cypher查询语言详解
数据库·rag·graphrag
龙仔7252 小时前
SQL Server 批量多库只读账号一键脚本(带逐行注释完整脚本 + 分段逐句详细解释)
服务器·数据库·sql·sqlserver·dba
咏方舟【长江支流】3 小时前
【开源】跨语言·跨平台·跨数据库(2) —— 用宝轻量框架设计必读
数据库·架构·开源·ai编程·咏方舟-长江支流·用宝框架