@@ -1506,3 +1506,54 @@ WHERE store3 IS NOT NULL
1506
1506
---
1507
1507
1508
1508
** [ ⬆ Back to Top] ( #sql-coding-challenges-for-beginners ) **
1509
+
1510
+ ## 45. Find the Team Size
1511
+
1512
+ Write a SQL query to find the team size of each of the employees. Return result table in any order. Here is an example:
1513
+
1514
+ ```
1515
+ Employee Table:
1516
+ +-------------+------------+
1517
+ | employee_id | team_id |
1518
+ +-------------+------------+
1519
+ | 1 | 8 |
1520
+ | 2 | 8 |
1521
+ | 3 | 8 |
1522
+ | 4 | 7 |
1523
+ | 5 | 9 |
1524
+ | 6 | 9 |
1525
+ +-------------+------------+
1526
+ ```
1527
+
1528
+ ```
1529
+ Result table:
1530
+ +-------------+------------+
1531
+ | employee_id | team_size |
1532
+ +-------------+------------+
1533
+ | 1 | 3 |
1534
+ | 2 | 3 |
1535
+ | 3 | 3 |
1536
+ | 4 | 1 |
1537
+ | 5 | 2 |
1538
+ | 6 | 2 |
1539
+ +-------------+------------+
1540
+ ```
1541
+
1542
+ <details ><summary >Solution</summary >
1543
+
1544
+ ``` sql
1545
+ SELECT employee .employee_id , team .team_size
1546
+ FROM employee
1547
+ LEFT JOIN (
1548
+ SELECT team_id, COUNT (employee_id) AS team_size
1549
+ FROM employee
1550
+ GROUP BY team_id
1551
+ ) team
1552
+ ON employee .team_id = team .team_id ;
1553
+ ```
1554
+
1555
+ </details >
1556
+
1557
+ ---
1558
+
1559
+ ** [ ⬆ Back to Top] ( #sql-coding-challenges-for-beginners ) **
0 commit comments