leetcode 数据库题解
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 d.Name as Department, e.Name as Employee, e.Salary from Employee as e ,(select DepartmentId, max(Salary) max from Employee group by DepartmentId) t,Department as d where e.Salary = t.max and e.DepartmentId = t.DepartmentId and d.Id = e.DepartmentId;
176. Second Highest Salary
题意:
Write a SQL query to get the second highest salary from the Employee table. +----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.
解法:Using max() will return a NULL if the value doesn't exist. So there is no need to UNION a NULL. Of course, if the second highest value is guaranteed to exist, using LIMIT 1,1 will be the best answer.
select max(Salary) as SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee);
197. Rising Temperature
题意:
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
| | -- | |
| | -- | |
| | -- | |
| | -- | |
+---------+------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
| |
| |
+----+
Subscribe to see which companies asked this question.
解法:
select a.Id as Id from Weather a, Weather b where to_days(a.Date)-to_days(b.Date) = 1 and a.Temperature > b.Temperature;
leetcode 数据库题解的更多相关文章
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode一句话题解
深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...
- [leetcode] 位操作题解
子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [ ...
- [leetcode/lintcode 题解] 一致性哈希 II · Consistent Hashing II
[题目描述] 在 Consistent Hashing I 中我们介绍了一个比较简单的一致性哈希算法,这个简单的版本有两个缺陷: 增加一台机器之后,数据全部从其中一台机器过来,这一台机器的读负载过大, ...
- LeetCode 中等题解(3)
34 在排序数组中查找元素的第一个和最后一个位置 Question 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂 ...
随机推荐
- 把Excel中的数据转换成Sql语句
假如excel表格中有A.B.C三列数据,希望导入到数据库users表中,相应的字段各自是name,sex,age ,在你的excel表格中添加一列.利用excel的公式自己主动生成sql语句,方法例 ...
- java总结(1)——java的特点
之前学习java仅仅是单纯的学习,老师要求学习.所以就学习.可是没有细致考虑过java的一些特别之处,比方,我们为什么要学习java而不是其它的语言,它必定有选择它的特点.所以,从如今開始,仅仅有埋头 ...
- Ubuntu12.04 Skype4.2 提示Skype can't connect,安装Skype4.3
最近几天Skype突然不能登录啦,以为是自己密码记错啦,重置啦一下密码,发现仍然提示”Skype can't connect“,我的版本是Ubuntu12.04 Skype4.2 尝试啦很多办法仍然不 ...
- GIS-004-Cesium版权信息隐藏
.cesium-widget-credits { display: none; } .cesium-viewer .cesium-widget-credits { display: none; }
- <linux系统c语言生成.so文件,生成64位可执行文件,在64位系统中运行32位的可执行文件>
1.linux 系统c语言生成.o文件,---->gcc -m64 -c -fPIC test.c -o test.o2.linux 系统c语言生成.so文件,----->gcc -sha ...
- 英语——'s和s'和s的区别
举个例子吧,student's 是表示学生的,名词单数形式的所有格students' 是表示学生们的,名词复数形式的所有格students 是表示学生们,名词的复数形式
- getOwnPropertyNames() & keys()
1.getOwnPropertyNames方法可以获得对象的所有属性名,并储存于一个数组当中. keys方法只能获取可遍历的属性名并储存于数组. 2.在完成notepad模块模拟的过程中使用了getO ...
- eui.Button 上放置两张图片
有时按钮会有一个需求.放置一个按钮底图,按钮文字是特效字体,也是一张图. 1 1个普通按钮 <e:Button label="> <e:skinName> < ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- [SQL] sql server中如何查看执行效率不高的语句
sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考.在测量功能时,先以下命令清除sql serve ...