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


    作者:霊梦
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    指针变量的*p,p以及&p的区别
    C 真正理解二级指针
    二叉树Bynary_Tree(2):二叉树的递归遍历
    二叉树Binary_Tree(1):二叉树及其数组实现
    栈stack(2):栈的链表实现
    栈stack(1):栈的数组实现
    队列queue(2):链表实现队列
    老猿学5G扫盲贴:3GPP规范中与计费相关的主要规范文档列表及下载链接
    老猿学5G扫盲贴:推荐三篇介绍HTTP2协议相关的文章
    老猿学5G扫盲贴:中移动的5G计费架构中Nchf'服务化接口以及CHF中的AGF
  • 原文地址:https://www.cnblogs.com/linmeng97blogwzhh/p/9187178.html
Copyright © 2011-2022 走看看