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 |
+----------+
 /* Write your T-SQL query statement below */
select a.Name AS Employee
from Employee a join Employee b
on a.ManagerId=b.Id
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 |
+------------+----------+--------+
 Select a.Name AS Department, b.Name AS Employee, b.Salary AS Salary
from Department a,
(
Select Name,Salary,E.DepartmentId from Employee E Join(
Select DepartmentId,Max(Salary) AS MaxSalary from Employee
Group by DepartmentId
) tmp
On E.DepartmentId=tmp.DepartmentId
where E.Salary=tmp.MaxSalary
)AS b
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 ('', 'Joe', '', '')
insert into Employee (Id, Name, Salary, DepartmentId) values ('', 'Henry', '', '')
insert into Employee (Id, Name, Salary, DepartmentId) values ('', 'Sam', '', '')
insert into Employee (Id, Name, Salary, DepartmentId) values ('', 'Max', '', '')
Truncate table Department
insert into Department (Id, Name) values ('', 'IT')
insert into Department (Id, Name) values ('', '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题目(第一弹)的更多相关文章

  1. RMQ_第一弹_Sparse Table

    title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...

  2. Oracle语法 及 SQL题目(三)

    目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...

  3. espcms代码审计第一弹

    以前的代码审计都是在CTF比赛题里面进行对于某一段代码的审计,对于后端php整体代码和后端整体架构了解的却很少,所以有空我都会学习php的代码审计,以提高自己 环境就直接用的是phpstudy,学习的 ...

  4. LeetCode缺失的第一个正数

    LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...

  5. leetcode二叉树题目总结

    leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...

  6. typecho流程原理和插件机制浅析(第一弹)

    typecho流程原理和插件机制浅析(第一弹) 兜兜 393 2014年03月28日 发布 推荐 5 推荐 收藏 24 收藏,3.5k 浏览 虽然新版本0.9在多次跳票后终于发布了,在漫长的等待里始终 ...

  7. 我的长大app开发教程第一弹:Fragment布局

    在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...

  8. Hadoop基础-MapReduce的工作原理第一弹

    Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...

  9. Java基础-程序流程控制第一弹(分支结构/选择结构)

    Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...

随机推荐

  1. [CSS3] Use Sticky Positioning for Section Headers

    We can take advantage of sticky positioning to keep a section header at the top of the page while th ...

  2. HDU 3572 Task Schedule(ISAP模板&&最大流问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  , ...

  3. U32670 小凯的数字 数学

    这是洛谷一个比赛中的一道题,和去年NOIP D1T1挺像.我看了一眼之后想“这不是小学奥数吗?求一个数字和就好了呀”...然后,60,剩下T了,gg. 只好看正解,但是一脸懵逼???然后看了证明,c* ...

  4. 电脑升级win10后visio的问题

    上周由于电脑意外蓝屏,系统从win7升级到了win10,昨天工作写文档时才发现缺少画图的工具,于是按照了visio2013,在编辑设计图时发现,一旦用visio打开或编辑图后再到word里设计图的内容 ...

  5. bzoj 1800 & 洛谷 P2165 [AHOI2009]飞行棋 —— 模拟

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1800   https://www.luogu.org/problemnew/show/P21 ...

  6. 三个命令解决ASTGO服务器重启后各种问题

    SSH 命令方式登录到服务器,依次执行下面三个命令. service httpd restart service mysqld restart safe_asterisk 前面两个命令提示无效,尝试从 ...

  7. CSS实现列表li边框重合问题

    CSS实现列表li边框重合问题 2017年04月13日 21:04:18 阅读数:5066 在我们写东西的时候经常要用到ul,但是也经常会出现li边框重合的问题,如下图:  可以看到每个格子的右边框和 ...

  8. js基本功能大全

    1.javascript的数组API: //定义数组 var pageIds = new Array(); pageIds.push('A'); 数组长度 pageIds.length; //shif ...

  9. go之数据类型转换和类型断言

    一.类型转换 1.1 简单类型转换 格式 valueOfTypeB = typeB(valueOfTypeA) int 转 float64 package main import "fmt& ...

  10. 数据连接类 这里采用mysql

    数据库通用操作类,自己参照几个通用类改写的,用起来还是蛮不错....  这里用的mysql 要是其他数据库自行更改下就好 public class MySqlHelper { public stati ...