• + 0 comments
    • MAX is used to pick the name in a grouped row.
    • ROW_NUMBER(): Assigns a unique number to each row.
    • PARTITION BY Occupation: Resets the row number for each occupation group.
    • ORDER BY Name: Sorts names alphabetically within each occupation group.

    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 SubQuery GROUP BY RN ORDER BY RN