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
|
1878 Discussions
|
Please Login in order to post a comment
Here's a simple solution in MySQL: Step 1: Calculate the salary of each student Step 2: Calulate the salary of each friend Step 3: Select the students where their salary is less than that of their friend
with student_salary as ( select s.id as id, s.name as name, sum(p.salary) as salary FROM students s inner join packages p on s.id = p.id group by s.id, s.name ), bestie_salary as ( select f.id as id, f.friend_id as bst_id, sum(p.salary) as salary from friends f inner join packages p on f.friend_id = p.id group by f.id, f.friend_id ), cte as ( select s.id as id, s.name as name, s.salary as student_salary, b.bst_id as friend_id, b.salary as friend_salary from student_salary s inner join bestie_salary b on s.id = b.id ) select name from cte where friend_salary > student_salary order by friend_salary asc
select student_name from (select s.id student_id, s.name student_name, p.salary student_salary,
f.friend_id friend_id, f.friend_name friend_name, f.friend_salary friend_salary from students s join packages p on p.id = s.id join (select ff.id, ff.friend_id, i.name as friend_name, pp.salary as friend_salary from friends ff join packages pp on pp.id = ff.friend_id join students i on i.id = ff.friend_id ) as f on f.id = s.id ) as mains where student_salary < friend_salary order by friend_salary
****select s.name from Students s join Friends f on s.id = f.id join Packages p on s.id = p.id join Packages fp on f.friend_id = fp.id where fp.salary > p.salary order by fp.salary;
SELECT s.Name FROM Students s JOIN Friends f ON s.ID = f.ID JOIN Packages p1 ON s.ID = p1.ID JOIN Packages p2 ON f.Friend_ID = p2.ID WHERE p2.Salary > p1.Salary ORDER BY p2.Salary;