코딩테스트/MySQL

[프로그래머스 Level 3] 물고기 종류 별 대어 찾기 (MySQL)

최-코드 2024. 11. 23. 17:39

https://school.programmers.co.kr/learn/courses/30/lessons/293261

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

처음에 아래와 같이 했더니 컴파일 에러가 발생했다.

select id, fish_name, max(length)
from fish_info natural join fish_name_info
group by fish_name
order by id

length를 max로 설정해줘서 이에 해당하는 id값으로 도출될 줄 알았지만, 안 되었고 따라서 아래와 같이 where 절에 서브쿼리로 두어서 max에 해당하는 length를 가지는 레코드를 찾도록 구현했다.

 

select id, fish_name, length
from fish_info natural join fish_name_info
where (fish_type, length) in (select fish_type, max(length)
                from fish_info natural join fish_name_info
                group by fish_type)
order by id