[SQL][HackerRank]The PADS


🔊このプレゼンテーションで使用する表の資料とソースはHackerRankです.
https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=false
🎈条件.
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).
OCCUPATIONSテーブルのすべての名前をアルファベット順にリストし、名前の後ろに各名前に対応するプレースホルダの最初の文字を表示します.
SELECT CONCAT(Name, '(', LEFT(Occupation,1), ')')
FROM OCCUPATIONS
ORDER BY Name ASC;
🎈条件.
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.
OCCUPATIONSテーブルで、使用された発生回数を問い合せます.発生回数に準じて昇順に並べ、以下の形式で出力します.
'There are a total of [occupation_count] [occupation]s.'
[職業count]は、OCCUPATIONSテーブル内の職業ごとの発生回数を表し、小文字で[職業]を出力します.
同じプレースホルダが1つ以上ある場合は、プレースホルダを昇順に並べます.
SELECT CONCAT('There are a total of ', count(*), ' ', Lower(Occupation),'s.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY count(*) ASC, Occupation ASC;
🎈問題解決の構想
CONCAT関数を使用して、
  • 文字列を結合します.
  • CONCAT(str1,str2,...)
  • プレースホルダの最初の文字を表示するには、LEFT関数を使用します.
  • LEFT(문자열,길이)
    LOWER関数を使用して、
  • 占領を小文字で出力します.