SQL

SQL) CONCAT

zzugest1 2023. 1. 10. 23:54

https://www.hackerrank.com/challenges/earnings-of-employees/problem?isFullScreen=true 

 

Top Earners | HackerRank

Find the maximum amount of money earned by any employee, as well as the number of top earners (people who have earned this amount).

www.hackerrank.com

We define an employee's total earnings to be their monthly Salary*Months worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.

 

Salary*Months값이 제일 큰 직원과 해당하는 직원수를 구하는 문제이다.

Sample Input

 

Sample Output

69952 1

 

이 문제를 풀던 중 결과 테이블이 다음과 같이 두개의 정답이 이어져 있는 것을 확인했다. 일반적인 방법으로 출력이 안되는 것을 알게 되어 다른 방법을 찾다가 문자열을 연결하는 함수 CONCAT과 연산자 ||을 알게 되었다.

 

* 위 문제는 CONCAT을 이용하지 않아도 되는 문제이다.

 

 

 

CONCAT (문자열1, 문자열2) : 두 문자열을 연결할 때 쓰이는 함수

 

예시

SELECT CONCAT(NAME,MONTHS) FROM Employee

위 코드는 NAME열과, MONTHS열을 연결해주는 코드이다.

Rose5
Angela7
Frank10
Patrick1

·

·

·

 

문제의 결과 테이블처럼 띄어쓰기를 할려면 다음과 같이 공백을 추가하면 된다.

SELECT CONCAT(NAME,' ',MONTHS) FROM Employee
Rose 5
Angela 7
Frank 10
Patrick 1

·

·

·

 

 

 

 

정답

SELECT MAX(SALARY *MONTHS), COUNT(EMPLOYEE_ID) FROM EMPLOYEE
GROUP BY (SALARY*MONTHS)
ORDER BY (SALARY*MONTHS) DESC LIMIT 1

 

'SQL' 카테고리의 다른 글

공통행이 없는 두 테이블의 JOIN  (0) 2023.01.12
맨하튼, 유클리드 거리 구하기  (0) 2023.01.12
정규표현식 REGEXP  (0) 2023.01.09
상품을 구매한 회원 비율 구하기  (0) 2023.01.09
UNION 응용  (0) 2023.01.07