/*
Walmart SQL Interview Question

Write a SQL query to output the names of those students whose best friends got higher salary package than student.
*/
    
-- Schema Setup
Create Table Students_Tbl (Id int,
Student_Name Varchar(30));

Insert into Students_Tbl values
(1,'Mark'),
(2,'David'),
(3,'John'),
(4,'Albert');

Create Table Friends_Tbl (Id int,
Friend_Id int);

Insert into Friends_Tbl values
(1,2),
(2,3),
(3,4),
(4,1);

Create Table Package_Tbl (Id int,
Salary Bigint );

Insert into Package_Tbl values
(1,18),
(2,12),
(3,13),
(4,15);

-- Solution
SELECT s.Student_Name
FROM Students_Tbl s
JOIN Friends_Tbl f ON s.Id = f.Id
JOIN Package_Tbl p1 ON s.Id = p1.Id
JOIN Package_Tbl p2 ON f.Friend_Id = p2.Id
WHERE p2.Salary > p1.Salary
ORDER BY s.Student_Name;



Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: