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
相关推荐
江湖一码农9 小时前
[小白]spring boot接入emqx
java·数据库·spring boot
HitpointNetSuite11 小时前
连锁餐饮行业ERP如何选择:为何Oracle NetSuite成为增长新引擎
大数据·运维·数据库·oracle·netsuite
冻咸鱼12 小时前
MySQL基础知识大全
数据库·mysql·oracle
emma羊羊12 小时前
【Redis】
数据库·redis·缓存
im_AMBER12 小时前
Leetcode 38
笔记·学习·算法·leetcode
Miraitowa_cheems13 小时前
LeetCode算法日记 - Day 82: 环形子数组的最大和
java·数据结构·算法·leetcode·决策树·线性回归·深度优先
仰泳的熊猫14 小时前
LeetCode:698. 划分为k个相等的子集
数据结构·c++·算法·leetcode
程序猿小蒜15 小时前
基于springboot的车辆管理系统设计与实现
java·数据库·spring boot·后端·spring·oracle
WBluuue15 小时前
数据结构与算法:摩尔投票算法
c++·算法·leetcode
数据库知识分享者小北15 小时前
如何构建企业级数据分析助手:Data Agent 开发实践
数据库·阿里云·1024程序员节·dataagent