Symmetric Pairs

  • + 0 comments
    WITH name_salary AS (
        SELECT s.id AS n_id, s.name AS name, p.salary AS nsalary
        FROM Students s
        JOIN Packages p
        USING (id)
    ),
    friend_salary AS (
        SELECT f.id AS id, f.friend_id AS f_id, p.salary AS fsalary
        FROM Friends f
        JOIN Packages p
        ON f.friend_id = p.id
    )
    SELECT ns.name
    FROM name_salary ns
    JOIN friend_salary fs
    ON ns.n_id = fs.id
    WHERE fs.fsalary > ns.nsalary
    ORDER BY fs.fsalary;