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.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂 ...
随机推荐
- Nginx伪静态配置和常用Rewrite伪静态规则集锦
伪静态是一种可以把文件后缀改成任何可能的一种方法,如果我想把php文件伪静态成html文件,这种相当简单的,下面我来介绍nginx 伪静态配置方法 nginx里使用伪静态是直接在nginx.conf ...
- makefile--变量的使用(二)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 仔细研究我们的之前Makefile发现,我们还有改进的地方,就是此处: target_bin : ...
- 超全面的JavaWeb笔记day03<JS对象&函数>
1.js的String对象(****) 2.js的Array对象 (****) 3.js的Date对象 (****) 获取当前的月 0-11,想要得到准确的月 +1 获取星期时候,星期日是 0 4.j ...
- 使用loadrunner进行压力测试遇到的问题总结
本人整理了一个LR使用过程中遇到的各种问题的总结文档,有需要可以加QQ群169974486下载. 一.无法生成虚拟用户,运行报错:CCI compilation error -vuser_init.c ...
- oracle常用管理命令
启动数据库和监听 lsnrctl start sqlplus /nolog conn sys/as sysdba startup 查看当前的实例名 show parameter instance_n ...
- 使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h"
使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h",解决方法如下: 1>.在xcode中左边选中项目的root节点,在中间编 ...
- 一次Win10安装体验
我下载的是win10 Build 14279版本.http://www.iwin10.com/xiazai/1071.html 下载之后就直接拷到U盘安装了. 安装完之后发现(因为我是分区成了两个)我 ...
- Spring学习笔记--声明一个简单的Bean
spring依赖的maven dependencyhttp://mvnrepository.com/artifact/org.springframework 在pom.xml中添加如下依赖: < ...
- Java初学者笔记二:关于类的常见知识点汇总
一.Java的类: Java的类是Java的基本概念了,基本的定义语法我就不提了,自己也不会忘了的,下面分成几个模块介绍: 1.Java的类定义时候的修饰符 2.Java的类的继承与派生 二.Java ...
- PostgreSQL远程代码执行漏洞(CVE-2018-1058)学习笔记
零.参考文献和绪论: 1.先知社区chybeta大神的--PostgreSQL 远程代码执行漏洞分析及利用—[CVE-2018-1058]--一文 2.博客园hunchill的--Mac 下 Post ...