LeetCode SQL题目(第一弹)

Posted 霊梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 



以上是关于LeetCode SQL题目(第一弹)的主要内容,如果未能解决你的问题,请参考以下文章

mysql数据库第一弹

JVM第一弹

JVM第一弹

日程管理系统代码维护第一弹

Object Oriented个人总结第一弹

ApiCloud+Vue.js浅扩展第一弹