Find The Team Size Problem


Description

LeetCode Problem 1303.

Table: Employee

1
2
3
4
5
6
7
8
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| employee_id   | int     |
| team_id       | int     |
+---------------+---------+
employee_id is the primary key for this table.
Each row of this table contains the ID of each employee and their respective team.

Write an SQL query to find the team size of each of the employees.

Return result table in any order.

The query result format is in the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Employee Table:
+-------------+------------+
| employee_id | team_id    |
+-------------+------------+
|     1       |     8      |
|     2       |     8      |
|     3       |     8      |
|     4       |     7      |
|     5       |     9      |
|     6       |     9      |
+-------------+------------+
Result table:
+-------------+------------+
| employee_id | team_size  |
+-------------+------------+
|     1       |     3      |
|     2       |     3      |
|     3       |     3      |
|     4       |     1      |
|     5       |     2      |
|     6       |     2      |
+-------------+------------+
Employees with Id 1,2,3 are part of a team with team_id = 8.
Employees with Id 4 is part of a team with team_id = 7.
Employees with Id 5,6 are part of a team with team_id = 9.


MySQL Solution

1
2
3
4
select e1.employee_id, count(e2.employee_id) as team_size
from Employee e1
inner join Employee e2 on e1.team_id = e2.team_id
group by e1.employee_id, e2.team_id




Related Posts

Number Of Transactions Per Visit Problem

LeetCode 1336. Write an SQL query to find how many...

Restaurant Growth Problem

LeetCode 1321. You are the restaurant owner and you want...

Running Total For Different Genders Problem

LeetCode 1308. Write an SQL query to find the total...

Ads Performance Problem

LeetCode 1322. A company is running Ads and wants to...

List The Products Ordered In A Period Problem

LeetCode 1327. Write an SQL query to get the names...

Find The Team Size Problem

LeetCode 1303. Write an SQL query to find the team...