• + 0 comments

    with fnd as( Select st.ID as ID, st.Name as Name, pk.Salary as Salary, f.Friend_ID, st1.Name as bf_name, pk1.Salary as bf_Sal from Students st join Packages pk on st.ID = pk.ID --- to get the student salary join Friends f on st.ID = f.ID --- to get the best Friend ID join Students st1 on st1.ID = f.Friend_ID -- to get the best friend Name join Packages pk1 on f.Friend_ID = pk1.ID --- to get the best friend Salary )

    Select Name from fnd where Salary < bf_Sal order by bf_Sal

    --- Name(output) < BestFriend (Salary offered) --- order by best Salary asc