African Cities, The Report, Top Competitors
African Cities 📝 ⭐️
https://www.hackerrank.com/challenges/weather-observation-station-20/problem?isFullScreen=true
➡️ station 테이블
- 출력 : lat_n의 중앙값 찾기 ( 소수점 4째자리에서 반올림)
- MySQL은 중앙값을 찾을 수 없기 때문에 PERCENT_RANK 함수를 활용하여 0.5인 lat_n 출력 되도록 하기
A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes(LAT_N) from STATION and round your answer to decimal places.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT ROUND(lat_n,4)
FROM
(SELECT lat_n,
PERCENT_RANK() OVER (ORDER BY lat_n) as percent
FROM station) AS s
WHERE s.percent = 0.5
The Report 📝 ⭐️
https://www.hackerrank.com/challenges/the-report/problem?isFullScreen=true
➡️ students,grades 테이블
- 출력 : 이름(grade가 8보다 작을 경우 null 출력) , grade, marks
- 조건 1️⃣ : grades 테이블과 students 테이블 조인 => min_mark <= mark <= max_mark 가기
- 조건 2️⃣ : ORDER BY grade, name, marks
You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks.
Grades contains the following data:
Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered first. If there is more than one student with the same grade (8-10) assigned to them, order those particular students by their name alphabetically. Finally, if the grade is lower than 8, use "NULL" as their name and list them by their grades in descending order. If there is more than one student with the same grade (1-7) assigned to them, order those particular students by their marks in ascending order.
Write a query to help Eve.
Sample Input
Sample Output
Maria 10 99
Jane 9 81
Julia 9 88
Scarlet 8 78
NULL 7 63
NULL 7 68
Note
Print "NULL" as the name if the grade is less than 8.
Explanation
Consider the following table with the grades assigned to the students:
So, the following students got 8, 9 or 10 grades:
- Maria (grade 10)
- Jane (grade 9)
- Julia (grade 9)
- Scarlet (grade 8)
SELECT IF(g.grade < 8, NULL, s.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, s.name, s.marks
Top Competitors 📝 ⭐️
https://www.hackerrank.com/challenges/full-score/problem?isFullScreen=true
➡️ hackers,difficulty,challenges,submissions 테이블
- 출력 : full score(만점) 2개 이상 받은 참가자 찾기
- 만점 기준 : submission 테이블 score = difficulty 테이블 score
- hackers,difficulty,challenges,submissions 조인 시킨 후에 hacker_id와 h.name으로 그룹 시킨 후에 hacker_id값이 두개이상인 값 출력하기
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.
Input Format
The following tables contain contest data:
- Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
- Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level.
- Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge.
- 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 that the submission belongs to, and score is the score of the submission.
Sample Input
Hackers Table:
Difficulty Table:
Challenges Table:
Submissions Table:
Sample Output
90411 Joe
Explanation
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 space-separated values.
SELECT h.hacker_id, h.name
FROM submissions s
INNER JOIN challenges c ON s.challenge_id = c.challenge_id
INNER JOIN difficulty d ON c.difficulty_level = d.difficulty_level
INNER JOIN hackers h ON s.hacker_id = h.hacker_id
WHERE s.score = d.score AND c.difficulty_level = d.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