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


原文地址:https://www.cnblogs.com/linmeng97blogwzhh/p/9187178.html

时间: 2024-09-29 07:46:02

LeetCode SQL题目(第一弹)的相关文章

【数据库优化(持续更新)】--第一弹设计优化

前言 数据库是程序的仓库,也是程序中最脆弱的一部分,因为它的脆弱性和重要性,所以需要专门进行管理和优化.在如今的网络化的时代更加需要数据库的灵活和快捷,一个高效的数据库能够使程序运行效率更快,提高程序的运行效率.但往往对数据库的设计达不到我们想要的效果,所以数据库的优化显得尤为重要.该系列文章正是考虑大数据量的当今如何才能让数据库的设计更加灵活,数据检索.操作更加高效展开的讨论,其中涉及到的优化方法是在笔者长期的开发经验以及其它有关数据库优化的文章基础上进行总结的,如果有异议还请指出. 数据

RMQ_第一弹_Sparse Table

title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 RMQ (Range Minimum/Maximum Query) 从英文便可以看出这个算法的主要是询问一个区间内的最值问题,,, 暑假集训的时候学习了 线段树 ,,, 也可以对给定数组查询任意区间的最值问题,,,, 这两个主要的区别就是 线段树 可以进行单点的修改操作,,,而 Sparse Ta

javascript之【贪吃蛇系列】第一弹:简单的贪吃蛇实现

参考博客:http://blog.csdn.net/sunxing007/article/details/4187038 以上博客是参考,毕竟第一次做,真让自己盲人摸象做不出来. 不过我在其上做了一些改进,界面等效果看起来更好一些. 下图是在Chrome上运行的效果,但是火狐和IE会不兼容,onkeydown事件不能正确调用 这里用了一张图把贪吃蛇制作过程的思想画了出来,画的有点简陋: 下面就是把代码发上来,上边有详细的解释: <html> <head> <title>

codechef 营养题 第一弹

第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5XXoERDanUcdxw08SmRj1a5VY1o7jpW1xYv_V1kuYao1Pg4yKdfG4MfNsNAEa codechef problems 第一弹 一.Authentication Failed原题题面Several days ago Chef decided to registe

FluentData 学习 第一弹

地址: http://fluentdata.codeplex.com/ 前世: FluentData 我们公司用的一个增删改查的里面的持久层.之前还不知道 这个持久层叫FluentData.  某天看见群里 说 某视频网站里面 居然在讲这个开发框架,还收费.我搜了一下.fluentdata有源代码. 这个13年有过记载.不过我是新手.什么也需要 学习一下.  和 室友说了一下微型orm ,他们 呢  用的  微型orm是 Dapper .可以去了解一下. 废话真多,完毕. FluentData

C#Light 和 uLua的对比第一弹

初始化 做一样的初始化,其实是没办法对等的 C#Light的Env 不等于Lua的LuaState C#Light的执行有完全的执行堆栈,完整的作用域,lua都没有 C#Light有完整的class 函数,继承特征,lua都没有 C#Light可以用VS做编辑器,lua? 为了保持平等对抗,三个测试先做一遍,再依次执行 测试1 ,简单的一次创建给个名字并销毁,测试300次 测试2,调用Debug.Log打印,执行300次 测试3,一次性在脚本里多调用一些方法,创建10个物体再销毁 测试一结果:

The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介

.net中的通讯方案很多,从.net Remoting,MSMQ,Webservice,WSE,WCF等等,他们都有一个特点,易用,但是都不能跨语种异构,如果你服务端要用java开发,客户端用C#开发,或者其它语言譬如C++, Python,PHP, Ruby, Objective-C,等等,那么.net提供的解决方案将不再有效.现在,隆重推出跨平台异构方案ICE.ICE官网:http://www.zeroc.com/ice.html 简介:The Internet Communications

MongoDB第一弹

下载: MongoDB下载地址: http://www.mongodb.org/ 我下载的是windows32的MSI. 安装: next就可以. 新建: 新建data文件夹,在data里面再建db和log两个子目录. 启动: 进入cmd,到当前MongoDB的bin目录下面,启动 "mongd"!(不是mongo),修改db路径. 然后打开浏览器输入localhost:27017,会出现: 最后在cmd中启动mongo.exe,出现: 基本操作: insert: find: upda

Ubuntu闪电入门第一弹

目录: 一.设置主机名 1.临时修改主机名 2.永久修改主机名 二.设置网络 1.配置静态IP地址 2.配置DNS服务器解析 3.修改DNS本地解析 三.软件包管理 1.dpkg管理软件包 2.apt管理软件包 四.运行级别 五.文件传输工具lrszs 1.发送文件sz 2.接收文件rz 六.磁盘管理 1.磁盘分区 2.磁盘分区格式化 3.挂载磁盘分区 一.设置主机名 1.临时修改主机名(重启系统后失效) $ sudo hostname ikki $ hostname ikki 2.永久修改主机