美文网首页
sql复习强化每日五题——第二天

sql复习强化每日五题——第二天

作者: 如虎添 | 来源:发表于2020-09-25 15:52 被阅读0次
    Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
    
    +----+-------+--------+-----------+
    | Id | Name  | Salary | ManagerId |
    +----+-------+--------+-----------+
    | 1  | Joe   | 70000  | 3         |
    | 2  | Henry | 80000  | 4         |
    | 3  | Sam   | 60000  | NULL      |
    | 4  | Max   | 90000  | NULL      |
    +----+-------+--------+-----------+
    给定 Employee 表,编写一个 SQL 查询,
    该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
    
    +----------+
    | Employee |
    +----------+
    | Joe      |
    +----------+
    
    答案
    select NameEmp as Employee from
    (
        select e1.Name as NameEmp,e1.Salary as SalaryEmp,
        e2.Name as NameMag,e2.Salary as SalaryMag
        from Employee e1 left join Employee e2
        on e1.ManagerId = e2.Id
        where e1.Salary > e2.Salary
    ) as newEmp
    
    编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
    
    示例:
    
    +----+---------+
    | Id | Email   |
    +----+---------+
    | 1  | a@b.com |
    | 2  | c@d.com |
    | 3  | a@b.com |
    +----+---------+
    根据以上输入,你的查询应返回以下结果:
    
    +---------+
    | Email   |
    +---------+
    | a@b.com |
    +---------+
    说明:所有电子邮箱都是小写字母。
    
    答案
    select Email from 
    (
      select Email,count(Email) as num from Person group by Email
    ) a 
    where num>=2
    
    某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
    
    Customers 表:
    
    +----+-------+
    | Id | Name  |
    +----+-------+
    | 1  | Joe   |
    | 2  | Henry |
    | 3  | Sam   |
    | 4  | Max   |
    +----+-------+
    Orders 表:
    
    +----+------------+
    | Id | CustomerId |
    +----+------------+
    | 1  | 3          |
    | 2  | 1          |
    +----+------------+
    例如给定上述表格,你的查询应返回:
    
    +-----------+
    | Customers |
    +-----------+
    | Henry     |
    | Max       |
    +-----------+
    
    答案
    select cts.Name as Customers
    from Customers cts left join Orders ods 
    on cts.Id = ods.CustomerId 
    where ods.Id is null
    
    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
    
    +----+-------+--------+--------------+
    | Id | Name  | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1  | Joe   | 70000  | 1            |
    | 2  | Jim   | 90000  | 1            |
    | 3  | Henry | 80000  | 2            |
    | 4  | Sam   | 60000  | 2            |
    | 5  | Max   | 90000  | 1            |
    +----+-------+--------+--------------+
    Department 表包含公司所有部门的信息。
    
    +----+----------+
    | Id | Name     |
    +----+----------+
    | 1  | IT       |
    | 2  | Sales    |
    +----+----------+
    编写一个 SQL 查询,找出每个部门工资最高的员工。对于上述表,您的 SQL 查询应返回以下行(行的顺序无关紧要)。
    
    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | IT         | Max      | 90000  |
    | IT         | Jim      | 90000  |
    | Sales      | Henry    | 80000  |
    +------------+----------+--------+
    解释:
    Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。
    
    答案
    select 
        Department,
        Employee,
        Salary 
    from 
    (
        select 
            dmt.Name as Department,
            emp.Name as Employee,
            emp.Salary,
        dense_rank() over
            (
                partition by emp.DepartmentId 
                order by emp.Salary DESC
            ) as _rank 
        from Employee emp left join Department dmt 
        on emp.DepartmentId = dmt.Id
    ) t
    where _rank = 1 and Department != "" 
    
    Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。
    
    +----+-------+--------+--------------+
    | Id | Name  | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1  | Joe   | 85000  | 1            |
    | 2  | Henry | 80000  | 2            |
    | 3  | Sam   | 60000  | 2            |
    | 4  | Max   | 90000  | 1            |
    | 5  | Janet | 69000  | 1            |
    | 6  | Randy | 85000  | 1            |
    | 7  | Will  | 70000  | 1            |
    +----+-------+--------+--------------+
    Department 表包含公司所有部门的信息。
    
    +----+----------+
    | Id | Name     |
    +----+----------+
    | 1  | IT       |
    | 2  | Sales    |
    +----+----------+
    编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:
    
    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | IT         | Max      | 90000  |
    | IT         | Randy    | 85000  |
    | IT         | Joe      | 85000  |
    | IT         | Will     | 70000  |
    | Sales      | Henry    | 80000  |
    | Sales      | Sam      | 60000  |
    +------------+----------+--------+
    解释:
    
    IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。
    销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二
    
    答案
    select 
        Department,
        Employee,
        Salary 
    from
    (
        select 
            dmt.Name as Department,
            emp.Name as Employee,
            emp.Salary as Salary,
            dense_rank() over(partition by emp.DepartmentId order by emp.Salary desc) as rank_ 
        from 
            Employee emp left join Department dmt 
            on emp.DepartmentId = dmt.Id 
            
    ) t 
    where rank_<=3 and Department != ""; 
    

    相关文章

      网友评论

          本文标题:sql复习强化每日五题——第二天

          本文链接:https://www.haomeiwen.com/subject/xjmxyktx.html