• + 0 comments

    MySQL

    SELECT d.name, p.name, s.name, a.name
    FROM (SELECT name, ROW_NUMBER() OVER (ORDER BY name ASC) AS id
          FROM OCCUPATIONS
          WHERE Occupation = "Doctor") AS d
    RIGHT JOIN (SELECT name, ROW_NUMBER() OVER (ORDER BY name ASC) AS id
          FROM OCCUPATIONS
          WHERE Occupation = "Professor") AS p ON d.id = p.id
    LEFT JOIN (SELECT name, ROW_NUMBER() OVER (ORDER BY name ASC) AS id
          FROM OCCUPATIONS
          WHERE Occupation = "Singer") AS s ON p.id = s.id
    LEFT JOIN (SELECT name, ROW_NUMBER() OVER (ORDER BY name ASC) AS id
          FROM OCCUPATIONS
          WHERE Occupation = "Actor") AS a ON p.id = a.id