[HackerRank] Advanced Select S1

Ga0·2023년 9월 5일
0

HackerRank

목록 보기
5/5

문제1 - Type of Triangle

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

Equilateral: It's a triangle with sides of equal length.
Isosceles: It's a triangle with sides of equal length.
Scalene: It's a triangle with sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a triangle.

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

문제2 - The PADS

Generate the following two result sets:

Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

There are a total of [occupation_count] [occupation]s.

where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.

Note: There will be at least two entries in the table for each type of occupation.

SELECT Name || '(' || CASE WHEN Occupation = 'Actor' THEN 'A' 
                        WHEN Occupation = 'Doctor' THEN 'D' 
                        WHEN Occupation = 'Professor' THEN 'P' 
                        WHEN Occupation = 'Singer' THEN 'S' END || ')'
 FROM OCCUPATIONS
 ORDER BY Name;
 
SELECT 'There are a total of ' || count(Occupation) || ' '|| LOWER(Occupation) || 's.'
 FROM OCCUPATIONS
 GROUP BY Occupation
 ORDER BY count(Occupation), Occupation;
  • LOWER() : 대문자를 소문자로 바꿔줌

문제3 - Occupations

Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.

Note: Print NULL when there are no more names corresponding to an occupation.

SELECT MAX(DECODE(OCCUPATION, 'Doctor', NAME))    Doctor
     , MAX(DECODE(OCCUPATION, 'Professor', NAME)) Professor
     , MAX(DECODE(OCCUPATION, 'Singer', NAME))    Singer
     , MAX(DECODE(OCCUPATION, 'Actor', NAME))     Actor
  FROM (
        SELECT OCCUPATION, NAME
             , ROW_NUMBER() OVER(PARTITION BY OCCUPATION ORDER BY NAME) Rownumber
          FROM OCCUPATIONS
       )
 GROUP BY Rownumber
 ORDER BY doctor, Professor, Singer, Actor
;
  • MAX() 대신 MIN()을 써도 무방하다. 피벗(PIVOT)을 하기 위해 합계함수를써준 것(문법상) => 어차피 Rownumber로 그룹화하기때문에 MAX()를 해도 해당 NAME컬럼값이 바뀌진 않는다.

0개의 댓글