题目
员工表:Employee
1 | +---------------+---------+ |
编写一个 SQL 查询,以求得每个员工所在团队的总人数。
查询结果中的顺序无特定要求。
查询结果格式示例如下:
1 | Employee Table: |
解法
解法一:
子查询
SQL
1 | select employee_id, (select count(*) from Employee e2 where e1.team_id = e2.team_id) as team_size from Employee e1 |
解法二:
左连接
SQL
1 | select e1.employee_id, count(*) team_size from employee e1 left join employee e2 on e1.team_id = e2.team_id group by e1.employee_id; |