# Students, Friends and Packages. Students contains two columns: ID and Name

You are given three tables: Students, Friends and Packages. Students contains two columns: ID and NameFriends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains two columns: ID and Salary (offered salary in \$ thousands per month).

## Students, Friends and Packages. Students contains two columns: ID and Name.

Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer.

Sample Input

Sample Output

``````Samantha
Julia
Scarlet
``````

Explanation

See the following table:

Now,

• Samantha’s best friend got offered a higher salary than her at 11.55
• Julia’s best friend got offered a higher salary than her at 12.12
• Scarlet’s best friend got offered a higher salary than her at 15.2
• Ashley’s best friend did NOT get offered a higher salary than her

The name output, when ordered by the salary offered to their friends, will be:

• Samantha
• Julia
• Scarlet

### Solution Students, Friends and Packages

Solution

``````CREATE TABLE students (id int, NAME VARCHAR(30));
CREATE TABLE packages (id int, salary INT);
CREATE TABLE friends (id int, friend_id INT);

INSERT INTO students values (1,'Ashley');
INSERT INTO students values (2,'Samantha');
INSERT INTO students values (3,'Julia');
INSERT INTO students values (4,'Scarlet');

INSERT INTO packages values (1,15.20);
INSERT INTO packages values (2,10.06);
INSERT INTO packages values (3,11.55);
INSERT INTO packages values (4,12.12);

INSERT INTO friends values (1,2);
INSERT INTO friends values (2,3);
INSERT INTO friends values (3,4);
INSERT INTO friends values (4,1);

SELECT * FROM students``````
``select s.name from students s, friends f, packages p, packages p2 where s.id = f.id and f.friend_id = p2.id and s.id = p.id and p.salary < p2.salary order by p2.salary;``

ORACLE Live SQL: https://livesql.oracle.com/apex/f?p=590:1000