1076. Project Employees II 難度:簡單

1、題目描述

Write an SQL query that reports all the projects that have the most employees.

The query result format is in the following example:

Project table:

project_id employee_id
1 1
1 2
1 3
2 1
2 4

Employee table:

employee_id name experience_years
1 Khaled 3
2 Ali 2
3 John 1
4 Doe 2

Result table:

project_id
1

The first project has 3 employees while the second one has 2.

來源:力扣(LeetCode)

2、解題思路

1# 主要是解決,出現多個最大值的情況
2# 首先增加子表,使用倒序和限制數量組合,得出最大值group by project_id order by count(employee_id) desc limit 1

select count(employee_id)
from Project
group by project_id
order by count(employee_id) desc
limit 1 

3# 然後,直接讓count(employee_id)等於上述值就行了

3、提交記錄

select project_id
from Project
group by project_id
having count(employee_id)=
(select count(employee_id)
from Project
group by project_id
order by count(employee_id) desc
limit 1 )
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章