• + 0 comments

    with CTE as (select Stds.ID,Stds.Name,Frnd.Friend_ID as Best_Friend_ID, Pk.Salary as Student_Salary, BFPk.Salary as Bestfriend_Salary from students Stds inner join Friends Frnd on Stds.ID = Frnd.ID inner join Packages PK on Pk.ID = Stds.ID inner join Packages BFPk on BFPk.ID = Frnd.Friend_ID), CTE_2 as ( select case When Bestfriend_Salary > Student_Salary Then Name end as bfmorethanstudent,Bestfriend_Salary from CTE) select bfmorethanstudent from CTE_2 where bfmorethanstudent is not null order by Bestfriend_Salary