Skip to content

Commit 4b114ef

Browse files
committed
Upload 184
1 parent 89b959b commit 4b114ef

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-183-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-184-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -217,6 +217,7 @@ Because this project necessitated a framework to enable consistent daily practic
217217
| 181 | [Customer Order Frequency](https://leetcode.com/problems/customer-order-frequency/description/) | [Solution](solutions/181_customer_order_frequency.md) | LeetCode | Easy | `USING`, `HAVING` |
218218
| 182 | [Low-Quality Problems](https://leetcode.com/problems/low-quality-problems/description/) | [Solution](solutions/182_low_quality_problems.md) | LeetCode | Easy | |
219219
| 183 | [Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii/description/) | [Solution](solutions/183_product_sales_analysis_ii.md) | LeetCode | Easy | |
220+
| 184 | [Project Employees II](https://leetcode.com/problems/project-employees-ii/description/) | [Solution](solutions/184_project_employees_ii.md) | LeetCode | Easy | Window function w/ `GROUP BY` |
220221
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
221222
<!--- cSpell:enable --->
222223

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SQL Everyday \#184
2+
3+
## Project Employees II
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Write a solution to report all the projects that have the most employees.
11+
12+
Return the resulting table in **any order**. [[Full Description](https://leetcode.com/problems/project-employees-ii/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte1 AS (
19+
SELECT
20+
p.project_id
21+
,COUNT(DISTINCT p.employee_id) AS emp_count
22+
FROM Project AS p
23+
JOIN Employee AS e ON p.employee_id = e.employee_id
24+
GROUP BY p.project_id
25+
),
26+
cte2 AS (
27+
SELECT
28+
project_id
29+
,RANK() OVER (ORDER BY emp_count DESC) AS ranked
30+
FROM cte1
31+
)
32+
SELECT
33+
project_id
34+
FROM cte2
35+
WHERE ranked = 1
36+
;
37+
```
38+
39+
## Site Solution
40+
41+
```sql
42+
-- LeetCode Solution
43+
-- None
44+
45+
-- Code Author: Jingjing Chen
46+
-- https://leetcode.com/problems/project-employees-ii/solutions/1096599/smart-window-function-solution-please-take-a-look
47+
WITH CTE AS (
48+
SELECT project_id, RANK() OVER(ORDER BY COUNT(employee_id) DESC) as ranking
49+
FROM Project
50+
GROUP BY project_id
51+
)
52+
53+
SELECT project_id
54+
FROM CTE
55+
WHERE ranking = 1
56+
ORDER BY project_id;
57+
58+
-- Code Author: Merciless
59+
-- https://leetcode.com/problems/project-employees-ii/solutions/307641/simple-sql-solution
60+
SELECT project_id
61+
FROM project
62+
GROUP BY project_id
63+
HAVING COUNT(employee_id) =
64+
(
65+
SELECT count(employee_id)
66+
FROM project
67+
GROUP BY project_id
68+
ORDER BY count(employee_id) desc
69+
LIMIT 1
70+
)
71+
72+
-- Code Author: Yaroslav Trofimov
73+
-- https://leetcode.com/problems/project-employees-ii/solutions/1476031/MAX
74+
SELECT project_id
75+
FROM Project
76+
GROUP BY project_id
77+
HAVING COUNT(employee_id) = (
78+
SELECT MAX(employees_count.employees_per_project)
79+
FROM (
80+
SELECT COUNT(employee_id) as employees_per_project
81+
FROM Project
82+
GROUP BY project_id
83+
) employees_count
84+
)
85+
```
86+
87+
## Notes
88+
89+
TBD
90+
91+
## NB
92+
93+
Window function w/ `GROUP BY`
94+
95+
Go to [Index](../?tab=readme-ov-file#index)\
96+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)