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
相关推荐
文火冰糖的硅基工坊2 小时前
[创业之路-653]:社会产品与服务的分类
大数据·数据库·人工智能
坚持编程的菜鸟2 小时前
LeetCode每日一题——交替合并字符串
c语言·算法·leetcode
235163 小时前
【MySQL】数据库事务深度解析:从四大特性到隔离级别的实现逻辑
java·数据库·后端·mysql·java-ee
脚踏实地的大梦想家3 小时前
【LangChain】P7 对话记忆完全指南:从原理到实战(下)
数据库·langchain
conkl3 小时前
Flask 与 MySQL 数据库集成:完整的 RESTful API 实现指南
数据库·mysql·flask
何中应3 小时前
MyBatis-Plus字段类型处理器使用
java·数据库·后端·mybatis
未知陨落4 小时前
LeetCode:77.买卖股票的最佳时机
算法·leetcode
迎風吹頭髮4 小时前
UNIX下C语言编程与实践21-UNIX 文件访问权限控制:st_mode 与权限宏的解析与应用
c语言·数据库·unix
炬火初现5 小时前
SQL语句——高级字符串函数 / 正则表达式 / 子句
数据库·sql
TTGGGFF5 小时前
云端服务器使用指南:利用Python操作mysql数据库
服务器·数据库·python