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
相关推荐
花自向阳开10246 小时前
LeetCode hot100-11
数据结构·算法·leetcode
月亮被咬碎成星星6 小时前
LeetCode[404]左叶子之和
算法·leetcode
_Itachi__9 小时前
LeetCode 热题 100 208. 实现 Trie (前缀树)
算法·leetcode·职场和发展
heart000_19 小时前
MySQL事务与锁机制详解:确保数据一致性的关键【MySQL系列】
数据库·mysql
一眼青苔9 小时前
MySQL 如何判断某个表中是否存在某个字段
数据库·mysql
闻闻不会编程9 小时前
74. 搜索二维矩阵 (力扣)
算法·leetcode·矩阵
西柚小萌新9 小时前
【大模型:知识图谱】--3.py2neo连接图数据库neo4j
数据库·知识图谱·neo4j
wangfenglei1234569 小时前
mybatis打印完整的SQL,p6spy
数据库·sql·mybatis
__风__9 小时前
PostgreSQL ERROR: out of shared memory处理
数据库·postgresql
占星安啦10 小时前
一个html实现数据库自定义查询
java·前端·javascript·数据库·动态查询