Prepare > SQL > Aggregation > Weather Observation Station 15

2023. 5. 18. 20:50HackerRank-My SQL

 

Weather Observation Station 15 | HackerRank

Query the Western Longitude for the largest Northern Latitude under 137.2345, rounded to 4 decimal places.

www.hackerrank.com

 

문제


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 4 decimal places.

 

 

 

 

코드


SELECT ROUND(LONG_W, 4) FROM STATION
WHERE LAT_N = 
(SELECT MAX(LAT_N) FROM STATION WHERE LAT_N < 137.2345);
  • 최댓값을 가진 테이블을 만들고 조회
  • WHERE LAT_N = MAX(LAT_N); 이딴식으로 쓰면 안됨

 

 

 

 

노트


SELECT ROUND(LONG_W, 4) FROM STATION
WHERE LAT_N < 137.2345
ORDER BY LAT_N DESC
LIMIT 0, 1;
  • 최댓값을 가진 데이터들에 어떻게 접근할 것인가. 유연한 사고.
  • 최댓값, 최솟값에 대한 접근은 ORDER BY 로도 가능하다.

 

 

두 번째 시도

SELECT ROUND(LONG_W,4)
FROM STATION
WHERE LAT_N < 137.2345
ORDER BY LAT_N DESC
LIMIT 0,1