The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+------+----------+-----------+----------+
|Id |Name |Department |ManagerId |
+------+----------+-----------+----------+
|101 |John |A |null |
|102 |Dan |A |101 |
|103 |James |A |101 |
|104 |Amy |A |101 |
|105 |Anne |A |101 |
|106 |Ron |B |101 |
+------+----------+-----------+----------+

Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:

+-------+
| Name |
+-------+
| John |
+-------+

Note:
No one would report to himself.

给一个员工表和一个经理表,员工表里有经理id列,查询出所有至少有5个直接汇报员工的经理。

解法1:

SELECT
Name
FROM
Employee AS t1 JOIN
(SELECT
ManagerId
FROM
Employee
GROUP BY ManagerId
HAVING COUNT(ManagerId) >= 5) AS t2
ON t1.Id = t2.ManagerId
;  

解法2:

with cte as(
select ManagerId from employee
group by ManagerId
having count(Id)>=5
) select Name from employee e join cte c on e.Id=c.ManagerId  

All LeetCode Questions List 题目汇总

570. Managers with at Least 5 Direct Reports 至少有5个直接汇报员工的经理的更多相关文章

  1. SQL练习——LeetCode解题和总结(1)

    只用于个人的学习和总结. 178. Rank Scores 一.表信息 二.题目信息 对上表中的成绩由高到低排序,并列出排名.当两个人获得相同分数时,取并列名次,且名词中无断档. Write a SQ ...

  2. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  3. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

  4. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  5. Sql practice

    employee表 数据准备 use tempdb go if OBJECT_ID('employee') is not null drop table employee ;with employee ...

  6. Understanding and Using HRMS Security in Oracle HRMS

    Understanding and Using HRMS Security in Oracle HRMS Product:Oracle Human Resources Minimum Version: ...

  7. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  8. Object Modeling

    https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/Objec ...

  9. [他山之石]Google's Project Oxygen Pumps Fresh Air Into Management

    The Project Oxygen team spent one year data-mining performance appraisals, employee surveys, nominat ...

随机推荐

  1. Django项目中使用qq第三方登录。

    使用qq登录的前提是已经在qq互联官网创建网站应用并获取到QQ互联中网站应用的APP ID和APP KEY 1,建路由 # qq登录 path('loginQq/',qq.loginQq,name=' ...

  2. ReentrantReadWriteLock中的锁降级

    锁降级指的是写锁降级为读锁. 因为读锁与读锁之间不互斥,如果是写锁与读锁或者是写锁与写锁就会互斥,所以由写锁变为读锁就降级了. 如果当前线程拥有写锁,然后将其释放,最后再获取读锁,这种并不能称之为锁降 ...

  3. children(),find()

    向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() find() jQuery children() 方法 children() 方法返回被选元素的 ...

  4. Spark API--Spark 分区

    一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...

  5. 修改Tomcat启动窗口的名称(Title)

    内容简介 有时在运行项目时,在同一服务器会启动多个Tomcat,很难区分某个tomcat运行的是哪个项目,或者想查看tomcat的端口号,只能去server.xml中查看. 如果能把Tomcat窗口的 ...

  6. Codeforces Round #605 (Div. 3) A. Three Friends(贪心)

    链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...

  7. [HTML5] Using HTMLPortalElement to improve MPA preformance

    For multi pages application, it is very slow to navgiate between page by page, because it needs to r ...

  8. Goexit

    package main import ( "fmt" "runtime" ) func test() { defer fmt.Println("cc ...

  9. LOJ P10004 智力大冲浪 题解

    每日一题 day37 打卡 Analysis 经典的带限期和罚款的单位时间任务调度问题 将 val 从大到小排序,优先处理罚款多的,将任务尽量安排在期限之前,并且靠后,如果找不到,则放在最后面 #in ...

  10. vue子组件与子组件之前传值-----最简单办法

    1.在main.js中定义一个值(红色为重点) new Vue({ el: '#app', data: { Bus: new Vue() }, router, store, render: h =&g ...