본문 바로가기

SQL

(SQL) - LeetCode (easy) 1075. Project Employees I

반응형

https://leetcode.com/problems/project-employees-i/description/

 

Project Employees I - LeetCode

Can you solve this real interview question? Project Employees I - Table: Project +-------------+---------+ | Column Name | Type | +-------------+---------+ | project_id | int | | employee_id | int | +-------------+---------+ (project_id, employee_id) is th

leetcode.com

집계함수와 join을 사용해보는 문제였습니다.

📕 풀이방법

📔 풀이과정

1. employee_id를 fk로 참조하므로 이를 on조건으로 join해 table을 형성합니다.

2. 이후 group by를 사용해 project_id별로 avg를 구하고 오름차순으로 정렬해줍니다.


📕 Code

📔 ANSI SQL

select project_id, round(avg(experience_years),2) as average_years 
from project p 
join employee e on p.employee_id = e.employee_id
group by project_id 
order by project_id

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.