JobHoppoer-SQL-570. Managers with at Least 5 Direct Reports

发现只是盲点

对于 aggregated stats 的 过滤,不需要费劲的加一层 WHERE COUNT_Id > N,而是直接用 HAVING predicate

总结: GROUP BY 之前用 WHERE; 之后用 HAVING,文章

莫名其妙,我居然用了SUM(id),而不是 COUNT(id),debug了半天没看出来。

# Write your MySQL query statement below

# SELECT name FROM
# (SELECT managerId
#  FROM Employee
#  GROUP BY managerId
#  HAVING SUM(id) >= 5) as M 
#  # HAVING COUNT(managerId) >= 5) as M 
# JOIN Employee E
# ON M.managerId = E.id;


# SELECT * 
# FROM Employee
#  WHERE managerId != 'None'
#  GROUP BY managerId
#  HAVING SUM(id) >= 5;


SELECT name FROM
(SELECT managerId 
 FROM Employee
 GROUP BY managerId
 HAVING COUNT(id) >= 5) as M 
JOIN Employee E
ON M.managerId = E.id;

你可能感兴趣的:(JobHopper-SQL,leetcode)