• + 0 comments

    WITH OwnSalaryTable AS ( SELECT f.ID, f.Friend_ID, p.Salary AS "My_salary" FROM Friends f JOIN Packages p ON f.ID = p.ID ), TotalSalaryTable AS ( SELECT ost.ID, ost.Friend_ID, ost.My_salary, p.Salary AS "Friend_salary" FROM OwnSalaryTable ost JOIN Packages p ON ost.Friend_ID = p.ID ) SELECT s.Name FROM Students s JOIN TotalSalaryTable tst ON s.ID = tst.ID WHERE tst.My_salary < tst.Friend_salary ORDER BY tst.Friend_salary