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 |
- +----------+
- /* 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题目(第一弹)的更多相关文章
- RMQ_第一弹_Sparse Table
title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...
- Oracle语法 及 SQL题目(三)
目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...
- espcms代码审计第一弹
以前的代码审计都是在CTF比赛题里面进行对于某一段代码的审计,对于后端php整体代码和后端整体架构了解的却很少,所以有空我都会学习php的代码审计,以提高自己 环境就直接用的是phpstudy,学习的 ...
- LeetCode缺失的第一个正数
LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...
- leetcode二叉树题目总结
leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...
- typecho流程原理和插件机制浅析(第一弹)
typecho流程原理和插件机制浅析(第一弹) 兜兜 393 2014年03月28日 发布 推荐 5 推荐 收藏 24 收藏,3.5k 浏览 虽然新版本0.9在多次跳票后终于发布了,在漫长的等待里始终 ...
- 我的长大app开发教程第一弹:Fragment布局
在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...
- Hadoop基础-MapReduce的工作原理第一弹
Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...
- Java基础-程序流程控制第一弹(分支结构/选择结构)
Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...
随机推荐
- HDU 5266 bc# 43 LCA+跳表
学了一发LCA的倍增算法+跳表维护. 先说说LCA倍增算法,思路是fa[i][j]求的是i结点的2^j倍的祖先,其中2^0就是父结点了.所以可以递推fa[i][j]=fa[fa[i][j-1]][j- ...
- 微信推送给服务器的XML消息解析-springmvc 解析xml数据流
微信推送给服务器的XML消息解析: 可以使用request.getInputStream(); 获取输入的消息流:但是需要自己解析流: spring mvc自带解析功能: controller中: @ ...
- Effective Java:对于全部对象都通用的方法
前言: 读这本书第1条规则的时候就感觉到这是一本非常好的书.可以把我们的Java功底提升一个档次,我还是比較推荐的.这里我主要就关于覆盖equals.hashCode和toString方法来做一个笔记 ...
- MFC 小知识总结四
1 PlaySound 播放WAV格式的音乐 This function plays a sound specified by a file name, resource, or system ev ...
- java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
1.错误描写叙述 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String service. ...
- Android 65K问题之Multidex原理分析及NoClassDefFoundError的解决方法
Android 65K问题相信困惑了不少人,尽管AS的出来能够通过分dex高速解决65K问题,可是同一时候也easy由于某些代码没有打包到MainDex里引起NoClassDefFoundError. ...
- CreateWindowEx和CreateWindow的区别
CreateWindowEx 函数功能:该函数创建一个具有扩展风格的重叠式窗口.弹出式窗口或子窗口,其他与 CreateWindow函数相同.关于创建窗口和其他参数的内容,请参看CreateWindo ...
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)【转】
本文转载自:http://blog.csdn.net/u011321546/article/details/9293547 首先要加头文件:iomanip 一:setprecision 作用:控制输出 ...
- CodeForces - 743D Chloe and pleasant prizes
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- B3300 [USACO2011 Feb]Best Parenthesis 模拟
这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...