zoukankan      html  css  js  c++  java
  • 【hackerrank】Placements

    题目如下:

    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).

    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

    解题思路:本题难度一般,涉及到三张表的联合。为了清晰易读,可以把Students和Packages做联合,查出每个学生的名字和工资,并记为结果集a;同时把Friends和Packages表做联合查询,查询每个朋友的工资,并记为结果集b;最后再对a和b做联合查询即可。

    代码如下:

    /*
    Enter your query here.
    */
    select a.S_Name from 
    (select s.Name as S_Name,s.ID as S_ID,p.salary as S_salary from Students s,Packages p where s.ID = p.ID) a,
    (select f.ID as F_ID,p.salary as F_salary from Friends f,Packages p where f.Friend_ID = p.ID) b
    where S_ID = F_ID and S_salary < F_salary order by F_salary;
  • 相关阅读:
    安装和使用Glassfish
    3GP 的 DASH设计原则与标准
    hdu 2686 Matrix 最小费用最大流 或 多线程DP
    [Win32]客户端程序
    二叉查找树中节点的删除。
    Android:获取屏幕完整宽高,包含状态栏
    Django学习总结之五模型
    畸形的从业观
    调试版本和发行版本
    jsp中常见的错误处理(未完待续)
  • 原文地址:https://www.cnblogs.com/seyjs/p/11381442.html
Copyright © 2011-2022 走看看