SQL

코테준비 | Top earners/Weather Observation Station 13~18(+해커랭크 SQL 모든 문제 해결 완료)

성장하는 쿠키의 로그 기록 2025. 2. 18. 10:08

☑️  Top Earners

[문제]

  • 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  space-separated integers.
  • 직원의 가장 높은 총 수입과 해당 수입을 받는 직원 수집계
  • 두 출력값은 스페이스 두개로 연결

[문제 풀이]

내 코드

SELECT CONCAT(max(months*salary),'  ',count(DISTINCT employee_id))
FROM EMPLOYEE
WHERE months*salary IN (SELECT max(months*salary) FROM EMPLOYEE )

 

 

한줄 포인트

  • 수입이 가장 큰 직원의 수 집계 → where 절 다중컬럼 서브쿼리로 해결
  • 출력값 한 셀에 연결하여 출력 → concat 함수 사용

☑️  Weather Observation Station 13-14,16

[문제]

  • 13번 : Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than  and less than . Truncate your answer to  decimal places.
  • 14번 : Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than . Round your answer to  decimal places.
  • 16번 : Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780 . Round your answer to  decimal places.

[문제 풀이]

내 코드

#Weather Observation Station 13 풀이

select round(sum(lat_n),4)
from station
where lat_n >38.7880 and lat_n <137.2345

#Weather Observation Station 14 풀이

select round(max(lat_n),4)
from station
where lat_n <137.2345

#Weather Observation Station 16 풀이

select round(min(lat_n),4)
from station
where lat_n >38.7780

 


☑️  Weather Observation Station 15

[문제]

  • Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345 . Round your answer to  decimal places.
  • 특정값보다 작은 조건에서의 lat_n 최대값의 long_w 값 조회

[문제 풀이]

내 코드

select round(long_w,4)
from station
where lat_n = (select max(lat_n)from station where lat_n< 137.2345 )

한줄 포인트

  • 집계값 필터링 시 → where 절 다중 컬럼 서브쿼리 

☑️  Weather Observation Station 18

[문제]


[문제 풀이]

내 코드

#맨하탄 거리 출력 
select round(abs(min(lat_n)-max(lat_n))+abs(min(long_w)-max(long_w)),4)
from station

 

한줄 포인트

  • 맨하탄 거리는 ,두 좌표의 이동 거리의 합임 : d=x2x1+y2y1
  • min,max라 값의 대소가 정해져 있긴하나 그냥 절대값으로 구해줌 

☑️ Weather Observation Station 19

[문제]


[문제 풀이]

내 코드

-- select round(sqrt(pow((max(lat_n)-min(lat_n)),2) + pow(max(long_w)-min(long_w))),4)
-- from station

select round(sqrt((max(lat_n)-min(lat_n))*(max(lat_n)-min(lat_n))+(max(long_w)-min(long_w))*(max(long_w)-min(long_w))),4)
from station

 

한줄 포인트

  • 유클리드 거리는 ,두 좌표의 직선거리임 : d=(x2x1)^+(y2y1​)^
  • 해커랭크는 mysql 5.x 버전이라 `pow` 제곱함수 쓸수없어 직접 곱하여 해결

해커랭크 58문제도 모두 해결 완료 했다!