Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
The CITY and COUNTRY tables are described as follows:
SELECT SUM(CITY.POPULATION)
FROM CITY
INNER JOIN COUNTRY ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE COUNTRY.CONTINENT = 'Asia';
Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
Input Format
The CITY and COUNTRY tables are described as follows:
SELECT CITY.NAME
FROM CITY
JOIN COUNTRY ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE COUNTRY.CONTINENT = 'Africa';
Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION))
FROM CITY
JOIN COUNTRY ON CITY.COUNTRYCODE = COUNTRY.CODE
GROUP BY COUNTRY.CONTINENT;
alias 사용
SELECT c2.CONTINENT
, FLOOR(AVG(c1.POPULATION))
FROM CITY c1
INNER JOIN COUNTRY c2 ON c1.COUNTRYCODE = c2.CODE
GROUP BY c2.CONTINENT
report containing three columns: Name, Grade and Mark.
- descending order by grade
- If same grade,
- 8-10 grade: order those particular students by their name alphabetically.
- 1-7 grade: order those particular students by their marks in ascending order.
- Print "NULL" as the name if the grade is less than 8.
Maria 10 99
Jane 9 81
Julia 9 88
Scarlet 8 78
NULL 7 63
NULL 7 68
SELECT IF(g.Grade < 8, NULL, s.Name), g.Grade, s.Marks
FROM Students AS s
INNER JOIN Grades AS g ON s.Marks BETWEEN g.Min_Mark AND g.Max_Mark
ORDER BY g.Grade DESC, s.Name ASC, s.Marks ASC;
처음 풀었을 때와 똑같이 풀면 놓치는 경우가 몇개 있었다.
SELECT에서 CASE WHEN
구문을 사용했다. 이 문제에서는 IF
와 같은 기능이지만, 일반적으로 CASE WHEN
이 더 유연한 것 같다.
SELECT
CASE WHEN g.Grade < 8 THEN NULL
ELSE s.Name
END
, g.Grade
, s.Marks
...
기본 정렬은 grade 내림차순이지만, 8등급 미만은 marks를, 8등급 이상이면 name을 추가로 오름차순 정렬해야한다.
이때 다시 CASE WHEN
으로 조건을 나눈다.
ORDER BY g.Grade DESC,
CASE WHEN g.Grade < 8 THEN s.Marks
ELSE s.Name
END ASC;
ORDER BY g.Grade DESC
, CASE WHEN g.Grade < 8 THEN s.Marks END ASC
, CASE WHEN g.Grade >= 8 THEN s.Name END ASC;
이때, ASC는 END 이후에 써야한다.
SELECT
CASE WHEN g.Grade < 8 THEN NULL
ELSE s.Name
END AS Name
, g.Grade
, s.Marks
FROM STUDENTS s
INNER JOIN GRADES g ON s.Marks BETWEEN g.Min_Mark AND g.Max_Mark
ORDER BY g.Grade DESC,
CASE WHEN g.Grade < 8 THEN s.Marks
ELSE s.Name
END ASC;
Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
The following tables contain contest data:
90411 Joe
Hacker 86870 got a score of 30 for challenge 71055 with a difficulty level of 2, so 86870 earned a full score for this challenge.
Hacker 90411 got a score of 30 for challenge 71055 with a difficulty level of 2, so 90411 earned a full score for this challenge.
Hacker 90411 got a score of 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge.
Only hacker 90411 managed to earn a full score for more than one challenge, so we print the their hacker_id and name as 2 space-separated values.
SELECT h.hacker_id, h.name
FROM Submissions AS s
INNER JOIN Challenges AS c ON s.challenge_id = c.challenge_id
INNER JOIN Difficulty AS d ON c.difficulty_level = d.difficulty_level
INNER JOIN Hackers AS h ON s.hacker_id = h.hacker_id
WHERE d.score = s.score AND d.difficulty_level = c.difficulty_level
GROUP BY h.hacker_id, h.name
HAVING COUNT(h.hacker_id) > 1
ORDER BY COUNT(h.hacker_id) DESC, h.hacker_id;
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.
Wands
Wands_Property
: The mapping between code and age is one-one, meaning that if there are two pairs, and , then and
9 45 1647 10
12 17 9897 10
1 20 3688 8
15 40 6018 7
19 20 7651 6
11 40 7587 5
10 20 504 5
18 40 3312 3
20 17 5689 3
5 45 6020 2
14 40 5408 1
The data for wands of age 45 (code 1):
The data for wands of age 40 (code 2):
The data for wands of age 20 (code 4):
The data for wands of age 17 (code 5):
SELECT id
, age
, coins_needed
, power
FROM Wands AS W
INNER JOIN Wands_Property AS WP ON W.code = WP.code
WHERE WP.is_evil = 0
AND W.coins_needed = (SELECT MIN(W2.coins_needed)
FROM Wands AS W2
INNER JOIN Wands_Property AS WP2 ON W2.code = WP2.code
WHERE W2.power = W.power
AND WP2.age = WP.age)
ORDER BY W.power DESC, WP.age DESC;
이 문제에서 MySQL은 구버전이라 윈도우 함수를 사용할 수 없기 때문에는 MS SQL Server 사용
age, power를 기준으로 그룹화하여 coin 오름차순으로 번호를 매겨보면:
SELECT w.id
, w.code
, w.coins_needed
, wp.age
, w.power
, ROW_NUMBER() OVER (PARTITION BY wp.age, w.power ORDER BY w.coins_needed ASC) AS rn
FROM Wands w
INNER JOIN Wands_Property wp ON w.code = wp.code
WHERE wp.is_evil = 0
즉, 윈도우 함수로 구한 row number = 1인 경우가 가장 적은 coin인 wand 이므로, 위의 쿼리를 FROM절 서브쿼리로 넣어서 row number = 1인 경우만 출력한다.
SELECT id
, age
, coins_needed
, power
FROM (
SELECT w.id
, wp.age
, w.coins_needed
, w.power
, ROW_NUMBER() OVER (PARTITION BY wp.age, w.power ORDER BY w.coins_needed ASC) AS rn
FROM Wands w
INNER JOIN Wands_Property wp ON w.code = wp.code
WHERE wp.is_evil = 0
) t
WHERE rn = 1
ORDER BY power DESC, age DESC
Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
The following tables contain challenge data:
Hackers Table:
Challenges Table:
21283 Angela 6
88255 Patrick 5
96196 Lisa 1
Hackers Table:
Challenges Table:
12299 Rose 6
34856 Angela 6
79345 Frank 4
80491 Patrick 3
81041 Lisa 1
For Sample Case 0, we can get the following details:
Students 5077 and 62743 both created 4 challenges, but the maximum number of challenges created is 6 so these students are excluded from the result.
For Sample Case 1, we can get the following details:
Students 12999 and 34856 both created 6 challenges. Because 6 is the maximum number of challenges created, these students are included in the result.
SELECT H.hacker_id, H.name, COUNT(*) AS challenges_created
FROM Hackers AS H
INNER JOIN Challenges AS C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING challenges_created IN (SELECT sub1.challenges_created
FROM (SELECT COUNT(*) AS challenges_created
FROM Challenges AS C2
GROUP BY C2.hacker_id) AS sub1
GROUP BY sub1.challenges_created
HAVING COUNT(*) = 1)
OR challenges_created = (SELECT MAX(sub2.challenges_created)
FROM (SELECT COUNT(*) AS challenges_created
FROM Challenges AS C3
GROUP BY C3.hacker_id) AS sub2)
ORDER BY challenges_created DESC, H.hacker_id ASC;
did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too!
The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id, name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id. Exclude all hackers with a total score of 0 from your result.
The following tables contain contest data:
Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge for which the submission belongs to, and score is the score of the submission.
Hackers Table:
Submissions Table:
4071 Rose 191
74842 Lisa 174
84072 Bonnie 100
4806 Angela 89
26071 Frank 85
80305 Kimberly 67
49438 Patrick 43
Hacker 4071 submitted solutions for challenges 19797 and 49593, so the total score .
Hacker 74842 submitted solutions for challenges 19797 and 63132, so the total score .
Hacker 84072 submitted solutions for challenges 49593 and 63132, so the total score .
The total scores for hackers 4806, 26071, 80305, and 49438 can be similarly calculated.
SELECT H.hacker_id, H.name, sub2.total_score
FROM (SELECT sub.hacker_id, SUM(max_score) AS total_score
FROM (SELECT hacker_id, MAX(score) AS max_score
FROM Submissions
GROUP BY hacker_id, challenge_id) AS sub
GROUP BY sub.hacker_id
HAVING total_score != 0) AS sub2
INNER JOIN Hackers H ON sub2.hacker_id = H.hacker_id
ORDER BY sub2.total_score DESC, H.hacker_id ASC;
우선 Submissions에서 해커 id별로, 챌린지 id별로 그룹화하여 최고점을 구함
SELECT hacker_id
, challenge_id
, MAX(score) score_max
FROM Submissions
GROUP BY hacker_id, challeng_id
그리고 그걸 FROM절 서브쿼리로 놓은 뒤 해커 정보(Hackers)와 JOIN
그리고, 다시 해커 id와 이름으로 그룹화 후 총점 0점을 제외하여 정렬
SELECT h.hacker_id
, h.name
, SUM(score_max) total_score -- 점수 합계를 구해야함
FROM (
SELECT hacker_id
, challenge_id
, MAX(score) score_max
FROM Submissions
GROUP BY hacker_id, challenge_id
) s
INNER JOIN Hackers h ON h.hacker_id = s.hacker_id
GROUP BY h.hacker_id, h.name
HAVING total_score != 0 -- GROUP BY 이후에 조건을 걸 때는 HAVING 사용
ORDER BY total_score DESC, h.hacker_id