We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
select CASE when occupation='Doctor' then CONCAT(name,'(D)') when occupation='Professor' then CONCAT(name,'(P)') when occupation='Singer' then CONCAT(name,'(S)') else CONCAT(name,'(A)') end as occ_nam from OCCUPATIONS order by name asc;
Nice work! Your solution definitely gets the job done. The only challenge with this approach is that it hard-codes the occupations, so if a new one gets added later, the query would need to be updated manually. Using something like LEFT(occupation,1) (or SUBSTR depending on the SQL flavor) can make it more flexible and maintainable since it automatically works for any occupation. But overall, this is a solid way to solve it with CASE!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Weather Observation Station 5
You are viewing a single comment's thread. Return to all comments →
select CASE when occupation='Doctor' then CONCAT(name,'(D)') when occupation='Professor' then CONCAT(name,'(P)') when occupation='Singer' then CONCAT(name,'(S)') else CONCAT(name,'(A)') end as occ_nam from OCCUPATIONS order by name asc;
Nice work! Your solution definitely gets the job done. The only challenge with this approach is that it hard-codes the occupations, so if a new one gets added later, the query would need to be updated manually. Using something like LEFT(occupation,1) (or SUBSTR depending on the SQL flavor) can make it more flexible and maintainable since it automatically works for any occupation. But overall, this is a solid way to solve it with CASE!