• + 0 comments

    With cte as(

    select f.id,f.friend_id,s.name,p.Salary from friends as f Left join students as s on f.id=s.id Left join Packages as p on f.id=p.id ), cte2 as( select distinct c1.salary,c2.name from cte as c1 join cte as c2 on c1.id=c2.friend_id where c1.salary>c2.salary

    )

    select name from cte2 order by salary asc