zoukankan      html  css  js  c++  java
  • LeetCode SQL题目(第一弹)

    LeetCode SQL题目 

     注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序

    不管是MS-SQL Server还是MySQL都需要登陆才能使用,我没有使用SSMS 或Workbench,而是直接使用sqlcmd,解决登陆问题可以参考这个链接(http://www.cnblogs.com/skynothing/archive/2010/08/26/1809125.html)感谢这位博主的帮助。

    181. Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

    +----+-------+--------+-----------+
    | Id | Name  | Salary | ManagerId |
    +----+-------+--------+-----------+
    | 1  | Joe   | 70000  | 3         |
    | 2  | Henry | 80000  | 4         |
    | 3  | Sam   | 60000  | NULL      |
    | 4  | Max   | 90000  | NULL      |
    +----+-------+--------+-----------+
    

    Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

    +----------+
    | Employee |
    +----------+
    | Joe      |
    +----------+
    
    1 /* Write your T-SQL query statement below */
    2 select a.Name AS Employee
    3 from Employee a join Employee b
    4 on a.ManagerId=b.Id
    5 where a.Salary>b.Salary

    184. Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

    +----+-------+--------+--------------+
    | Id | Name  | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1  | Joe   | 70000  | 1            |
    | 2  | Henry | 80000  | 2            |
    | 3  | Sam   | 60000  | 2            |
    | 4  | Max   | 90000  | 1            |
    +----+-------+--------+--------------+
    

    The Department table holds all departments of the company.

    +----+----------+
    | Id | Name     |
    +----+----------+
    | 1  | IT       |
    | 2  | Sales    |
    +----+----------+
    

    Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | IT         | Max      | 90000  |
    | Sales      | Henry    | 80000  |
    +------------+----------+--------+

     1 Select a.Name AS Department, b.Name AS Employee, b.Salary AS Salary
     2 from Department a,
     3     (
     4     Select Name,Salary,E.DepartmentId from Employee E Join(
     5     Select DepartmentId,Max(Salary) AS MaxSalary from Employee
     6         Group by DepartmentId
     7     )   tmp
     8         On E.DepartmentId=tmp.DepartmentId
     9         where E.Salary=tmp.MaxSalary
    10     )AS b
    11 where a.id=b.DepartmentId

    185. Department Top Three Salaries

    以下为数据文件架构

    Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)
    Create table If Not Exists Department (Id int, Name varchar(255))
    Truncate table Employee
    insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1')
    insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2')
    insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2')
    insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1')
    Truncate table Department
    insert into Department (Id, Name) values ('1', 'IT')
    insert into Department (Id, Name) values ('2', 'Sales')

    The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

    +----+-------+--------+--------------+
    | Id | Name  | Salary | DepartmentId |
    +----+-------+--------+--------------+
    | 1  | Joe   | 70000  | 1            |
    | 2  | Henry | 80000  | 2            |
    | 3  | Sam   | 60000  | 2            |
    | 4  | Max   | 90000  | 1            |
    | 5  | Janet | 69000  | 1            |
    | 6  | Randy | 85000  | 1            |
    +----+-------+--------+--------------+
    

    The Department table holds all departments of the company.

    +----+----------+
    | Id | Name     |
    +----+----------+
    | 1  | IT       |
    | 2  | Sales    |
    +----+----------+
    

    Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

    +------------+----------+--------+
    | Department | Employee | Salary |
    +------------+----------+--------+
    | IT         | Max      | 90000  |
    | IT         | Randy    | 85000  |
    | IT         | Joe      | 70000  |
    | Sales      | Henry    | 80000  |
    | Sales      | Sam      | 60000  |
    +------------+----------+--------+
    
    解答如下:

    /* Write your T-SQL query statement below */
    select d.name AS Department, e.Name AS Employee,e.Salary AS Salary
    from Employee e, Department d 
    where  (
        select count(distinct(Salary))
        from Employee
        where DepartmentId=e.DepartmentId and Salary >e.Salary
        )in(0,1,2)
    and e.DepartmentId=d.Id
    Order by e.DepartmentId, e.Salary desc


    作者:霊梦
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    AUDIT审计的一些使用
    HOW TO PERFORM BLOCK MEDIA RECOVERY (BMR) WHEN BACKUPS ARE NOT TAKEN BY RMAN. (Doc ID 342972.1)
    使用BBED理解和修改Oracle数据块
    Using Class of Secure Transport (COST) to Restrict Instance Registration in Oracle RAC [ID 1340831.1]
    调试利器GDB概念
    第4章 思科IOS
    第3章 ip地址和子网划分
    第2章 TCPIP
    2020年阅读过的黑客资源推荐篇
    第1章 计算机网络
  • 原文地址:https://www.cnblogs.com/linmeng97blogwzhh/p/9187178.html
Copyright © 2011-2022 走看看