인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

joyee3421331's profile image
joyee3421331

asked

[A hundred words are not as good as seeing once] Advanced SQL for data analysis

Subquery HackerRank Problem Solving

해커랭크 Top Earners 질문!

Written on

·

289

0

안녕하세요! 해커랭크 Top Earners 문제에 관해 질문드리겠습니다.

 

where 절에 서브쿼리를 사용할 때,

select salary*months as earnings, count(*)

from employee

where salary*months = (select max(salary*months) from employee)

 

위의 코드를 작성했을때 group by 없이는 집계함수를 사용할 수 없다는 오류가 뜨는데,

 

select count(*)

from employee

where salary*months = (select max(salary*months) from employee)

 

select 에 count만 출력했을 때는 문제 없이 코드가 실행됩니다!

count 단독으로 출력할 때는 전체 갯수만 파악하지만 salary*months 까지 같이 출력할 때는 해당 salary*months에 맞게 count를 해야해서 집계함수를 사용해야 하는 걸까요?

어짜피 max값만 가져와서 출력하는데도 group by를 꼭 사용해야 하는걸까요?

 

sql

Answer 1

0

jaemin님의 프로필 이미지
jaemin
Instructor

count 단독으로 출력할 때는 전체 갯수만 파악하지만 salary*months 까지 같이 출력할 때는 해당 salary*months에 맞게 count를 해야해서 집계함수를 사용해야 하는 걸까요?
-> 맞습니다. select 절에 집계함수(count)와 집계함수가 아닌 컬럼이 같이 들어갈 경우, 컬럼을 기준으로 묶은 그룹마다 집계를 하는 것이 됩니다.
group by를 사용하고 싶지 않다면 두번째 쿼리처럼 작성하시면 됩니다.

joyee3421331's profile image
joyee3421331

asked

Ask a question