We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Join
- Placements
- Discussions
Placements
Placements
Sort by
recency
|
1526 Discussions
|
Please Login in order to post a comment
select a.name from friends b join packages c on b.friend_id=c.id join packages d on b.id=d.id join students a on a.id=b.id where C.salary > d.salary order by c.salary
SQL Code -
This is the shortest way possible!
WITH CTE AS(
SELECT Stu.id, Stu.Name, Pck.Salary as Stu_Salary, Fri.Friend_ID , Pck2.Salary as Fri_Salary
FROM Students as Stu INNER JOIN Friends as Fri ON Stu.id = Fri.ID INNER JOIN Packages as Pck ON Stu.id = Pck.ID INNER JOIN Packages as Pck2 ON Fri.Friend_ID = Pck2.ID)
SELECT Name FROM CTE WHERE Fri_Salary>Stu_Salary ORDER BY Fri_Salary ASC
WITH friend AS( SELECT Friends.id as id1, friend_id, Salary as salary1 FROM Friends JOIN Packages ON Friends.friend_id = Packages.id),
student AS ( SELECT Students.id as id2, Name, Salary as salary2 FROM Students JOIN Packages ON Students.id = Packages.id)
SELECT Name FROM student JOIN friend ON friend.id1 = student.id2 WHERE salary1 > salary2 ORDER BY salary1