Red Glitter Pointer

 

https://www.hackerrank.com/challenges/african-cities/problem?isFullScreen=true

 

African Cities | HackerRank

Query the names of all cities on the continent 'Africa'.

www.hackerrank.com

 

 

 

SELECT city.name
FROM city
    INNER JOIN country ON city.countrycode = country.code
WHERE country.continent = 'Africa'

 

 

https://leetcode.com/problems/reformat-department-table/description/

 

 

SELECT id
        ,SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue
        ,SUM(CASE WHEN month = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue
        ,SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue
        ,SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue
        ,SUM(CASE WHEN month = 'May' THEN revenue ELSE NULL END) AS May_Revenue
        ,SUM(CASE WHEN month = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue
        ,SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue
        ,SUM(CASE WHEN month = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue
        ,SUM(CASE WHEN month = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue
        ,SUM(CASE WHEN month = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue
        ,SUM(CASE WHEN month = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue
        ,SUM(CASE WHEN month = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
FROM department
GROUP BY id

 

 

느낀 점 및 정리 ✍️

1. CASE를 활용하여 피벗 테이블을 만들었다

2. 예를 들어, month의 값이 Jan인 revenue의 값들을 합산하여 Jan_Revenue에 값을 넣어주는 것이다.

 

 

 

Type of Triangle | HackerRank

Query a triangle's type based on its side lengths.

www.hackerrank.com

 

 

SELECT CASE
    WHEN A = B AND B = C THEN 'Equilateral'
    WHEN A + B <= C OR B + C <= A OR C + A <= B THEN 'Not A Triangle'
    WHEN A = B OR B = C OR A = C THEN 'Isosceles'
    ELSE 'Scalene'
    END
FROM triangles

 

느낀 점 및 정리 ✍️

1. 정삼각형인지 확인 후 Equilateral, 정삼각형이 아니라면 삼각형 조건에 부합하는지 먼저 확인한다. Not A Triangle

2. 삼각형 조건에 부합한다면 두 변의 길이가 같은지 판단, 같다면 Isosceles

3. 아니라면 세 변의 길이가 다 다른 것임 ! Scalene

4. 삼각형 조건에 충족되지 않는데 두 변의 길이가 같을 수 있기 때문에 두 변의 길이가 같은지 확인하기 전에 삼각형 조건 먼저 확인해줘야 함! 

 

 

 

 

Top Earners | HackerRank

Find the maximum amount of money earned by any employee, as well as the number of top earners (people who have earned this amount).

www.hackerrank.com

 

 

 

 

SELECT salary * months as earnings
    ,COUNT(*)
FROM employee
GROUP BY earnings
ORDER BY earnings DESC
LIMIT 1

 

 

느낀 점 및 정리 ✍️

1. maximum total earnings => Max(salary * months)

2. total number of employees who have maximum total earnings => GROUP BY earnings , COUNT(*)

 

 

 

 

Revising Aggregations - Averages | HackerRank

Query the average population of all cities in the District of California.

www.hackerrank.com

 

 

SELECT AVG(population)
FROM city
WHERE district = 'California'

 

 

 

https://leetcode.com/problems/duplicate-emails/description/

 

 

 

👇 정답 코드

SELECT Email
FROM Person
GROUP BY Email
HAVING COUNT(id) > 1

 

 

https://leetcode.com/problems/big-countries/description/

 

 

 

SELECT name, population, area
FROM world
WHERE area >= 3000000
OR population >= 25000000

 

 

 

 

https://leetcode.com/problems/not-boring-movies/description/

 

 

 

 

SELECT *
FROM cinema
WHERE description <> 'boring'
AND MOD(id, 2) = 1
ORDER BY rating DESC

 

느낀 점 및 정리 ✍️

1. odd-numbered ID => MOD(id, 2) = 1

2. description that is not boring => <> 'boring'

3. ordered by rating in descending order => ORDER BY rating DESC

 

 

+ Recent posts

loading