Saturday, April 17, 2021

Hacker Rank SQL Practice

Solutions to problem : The PADS

There are different ways to solve this 

First : Using SUBSTR and || 

select Name || '(' || UPPER(SUBSTR(Occupation, 1, 1)) || ')' 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;


Second: Using CASE and CONCAT

select Name|| (CASE Occupation 
WHEN 'Professor' THEN CONCAT(CONCAT('(', 'P'), ')')
WHEN 'Actor'     THEN CONCAT(CONCAT('(', 'A'), ')')
WHEN 'Doctor'    THEN CONCAT(CONCAT('(', 'D'), ')')
WHEN 'Singer'    THEN CONCAT(CONCAT('(', 'S'), ')')
END) Occupation from OCCUPATIONS order by Name ;


select 'There are a total of', count(Occupation), CONCAT(LOWER(Occupation),'s.') from OCCUPATIONS group by Occupation order by count(Occupation), Occupation ;


Friday, April 16, 2021

Dual Table in Oracle


Source : https://www.oracletutorial.com/oracle-basics/