LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 上
题目:1731. 每位经理的下属员工数量
题解:
select employee_id,name,reports_count,average_age
from Employees t1,(select reports_to,count(*) reports_count,round(avg(age)) average_agefrom Employeeswhere reports_to is not nullgroup by reports_to
) t2
where t1.employee_id=t2.reports_to
order by employee_id asc
题目:1789. 员工的直属部门
题解:
select employee_id,department_id from Employee
group by employee_id
having count(department_id)=1union select employee_id,department_id from Employee
where primary_flag='Y'
select employee_id,department_id from Employee
where primary_flag='Y' or employee_id in (select employee_id from Employeegroup by employee_idhaving count(department_id)=1
)
题目:610. 判断三角形
题解:
select x,y,z,
if(x+y>z and x+z>y and y+z>x,'Yes','No') triangle
from Triangle
select x, y, z,casewhen x+y>z and x+z>y and y+z>x then 'Yes'else 'No'end as triangle
from Triangle
题目:180. 连续出现的数字
题解:
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