코드카타 170-173
SELECT ROUND(MEDIAN(LAT_N),4)
FROM STATION;
SELECT ROUND(A.LAT_N,4)
FROM (
SELECT LAT_N, PERCENT_RANK() OVER(ORDER BY LAT_N)PR
FROM STATION
)A
WHERE PR = 0.5;
중앙값구하기
내가 푼풀이와 달리 푸는법에 대한 추가적인 설명.
Write a query to help Eve.
SELECT IF(G.GRADE <8 , NULL, S.NAME), G.GRADE, S.MARKS
FROM STUDENTS S JOIN GRADES G
ON S.MARKS BETWEEN MIN_MARK AND MAX_MARK
ORDER BY 2 DESC, 1,3;
SELECT H.hacker_id , H.NAME
FROM Submissions S
JOIN Hackers H ON S.hacker_id = H.hacker_id
JOIN Challenges C ON S.challenge_id = C.challenge_id
JOIN Difficulty D ON C.difficulty_level = D.difficulty_level
WHERE S.SCORE = D.SCORE
GROUP BY 1,2
HAVING COUNT(H.NAME) >= 2
ORDER BY COUNT(H.NAME)DESC, 1;
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.
문제 해석
1) id, age, coins_needed, power of the wands 출력하기
2) age와 power가 같은 것이 여러개라면 그 중 coins_needed 값이 가장 작은 것을 출력하기
3) is_evil 값이 0이어야 함
3) power순으로 내림차순, power가 같다면 age순으로 내림차순 하기
SELECT id, age, coins_needed, power
FROM Wands W JOIN Wands_Property WP
ON W.code = WP.code
WHERE is_evil = 0
AND coins_needed =(SELECT MIN(coins_needed)
FROM Wands W1 JOIN Wands_Property WP1
ON W1.code = WP1.code
WHERE is_evil = 0
AND WP1.age = WP.age
AND W1.power = W.power)
ORDER BY 4 DESC, 2 DESC;
다른 풀이.
SELECT w.id
, wp.age
, a.min_coins
, a.power -- (3)
FROM (SELECT code, power, MIN(coins_needed) AS min_coins
FROM wands
GROUP BY code, power) AS a
LEFT JOIN wands AS w
ON a.min_coins = w.coins_needed AND a.code = w.code
LEFT JOIN wands_property AS wp
ON a.code = wp.code -- (1)
WHERE wp.is_evil = 0
ORDER BY a.power DESC, wp.age DESC; -- (2)
인사이트
문제를 해석하는 과정에서 시간이 많이 소요되었다.
172번의 경우 여러개의 테이블을 조인하는 과정이 복잡해보이지만, S.SCORE = D.SCORE이 조건을 생각해내면 생각보다는 쉽게 풀리는 문제였다.
그리고 173번의 경우는 문제를 해석하는게 쉽지 않았고, 더 나아가 쿼리문도 다소 복잡했다.