Occupations Problem

The problem context involves a database table named 'OCCUPATIONS' which contains two fields: 'Name' and 'Occupation'. The task is to sort the names based on their respective occupations, which include 'Doctor', 'Professor', 'Singer', and 'Actor'.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Occupations Problem HackerRank solution using CodeRankGPT:

The solution approach involves using a subquery to assign a row number to each entry, partitioned by occupation and ordered by name. Then, in the main query, the MAX function is used in combination with a CASE statement to select the maximum (i.e., last) name for each occupation. The result is a list of names sorted by their respective occupations.


SELECT 
    MAX(CASE WHEN Occupation = 'Doctor' THEN Name END) AS Doctor,
    MAX(CASE WHEN Occupation = 'Professor' THEN Name END) AS Professor,
    MAX(CASE WHEN Occupation = 'Singer' THEN Name END) AS Singer,
    MAX(CASE WHEN Occupation = 'Actor' THEN Name END) AS Actor
FROM (
    SELECT 
        Name,
        Occupation,
        ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS rn
    FROM OCCUPATIONS
) AS sub
GROUP BY rn;

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform