• + 0 comments

    WITH FriendsSalary AS ( SELECT s.ID ,p.Salary FROM Students s JOIN Friends f ON f.ID=s.ID JOIN Packages p ON p.ID=f.Friend_ID ), StudentsSalary AS ( SELECT s.ID ,s.Name,p.Salary FROM Students s JOIN Friends f ON f.ID=s.ID JOIN Packages p ON p.ID=s.ID ) SELECT s.Name from StudentsSalary s JOIN FriendsSalary f ON f.ID=s.ID where f.Salary>s.Salary ORDER BY f.Salary