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
相关推荐
前端小白在前进17 分钟前
⭐力扣刷题:螺旋矩阵
算法·leetcode·矩阵
CoderYanger6 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
廋到被风吹走6 小时前
【数据库】【MySQL】InnoDB外键解析:约束机制、性能影响与最佳实践
android·数据库·mysql
掘根6 小时前
【消息队列】交换机数据管理实现
网络·数据库
Logic1017 小时前
《Mysql数据库应用》 第2版 郭文明 实验6 数据库系统维护核心操作与思路解析
数据库·sql·mysql·学习笔记·计算机网络技术·形考作业·国家开放大学
AI Echoes7 小时前
构建一个LangChain RAG应用
数据库·python·langchain·prompt·agent
@nengdoudou8 小时前
KingbaseES支持 mysql 的find_in_set函数
数据库·mysql
摇滚侠8 小时前
面试实战 问题三十三 Spring 事务常用注解
数据库·spring·面试
梁萌8 小时前
保姆级的MySQL执行计划(Explain)解读
数据库·mysql·explain·执行计划
JIngJaneIL8 小时前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot