[HackerRank][SQL(Oracle)] Placements

728x90

 

※  You are given three tables: Students, Friends and Packages. Students contains two columns: ID and Name. Friends 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).

 

■  [Advanced Join]  Placements

https://www.hackerrank.com/challenges/placements/problem?isFullScreen=true 

Q.

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.

A.

SELECT
	S.NAME
FROM
	STUDENTS S
    ,(
        SELECT
            F.ID			AS MY_ID
            ,P1.SALARY	AS MY_SAL
            ,F.FRIEND_ID	AS FND_ID
            ,P2.SALARY	AS FND_SAL
        FROM
            FRIENDS F
            , PACKAGES P1
            , PACKAGES P2
        WHERE    1=1
            AND F.ID = P1.ID
            AND F.FRIEND_ID = P2.ID
            AND P1.SALARY < P2.SALARY
    ) F_P_FL
WHERE   1=1
	AND S.ID = F_P_FL.MY_ID
ORDER BY
	F_P_FL.FND_SAL;

 

 

반응형