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.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂 ...
随机推荐
- 理解ros话题--6
理解ROS话题(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) Description: 本教程介绍ROS话题(topics)以及如何使用ro ...
- CentOS7.1 Liberty云平台之Dashboard篇(7)
控制节点: 一.安装及配置Dashboard 1.安装dashboard相关包 yum install openstack-dashboard 2.配置/etc/openstack-dashboard ...
- HTML5开发之 -- 模态突出窗(bootstrap)
最近在学习web端开发相关,bootstrap非常好用! 有个模态弹出窗的效果,在此记录下: 1.导入: <script src="libs/js/jquery-3.2.1.min.j ...
- django 错误分类及解决办法汇总
问题1:启动服务器后浏览器无法访问http://localhost:8000,访问被拒绝
- 演示PostgreSQL的详细安装及配置图解
右击文件选择以管理员身份运行 2 开始执行程序的安装 3 设置安装目录 4 设置数据的保存目录 5 设置数据库管理员密码,请牢记此密码. 6 设置端口号,选择默认的端口号即可 7 根据自己选择设置地区 ...
- D3D游戏编程系列(四):自己动手编写即时战略游戏之网络同步
说到网络同步,这真是一个网络游戏的重中之重,一个好的网络同步机制,可以让玩家的用户体验感飙升,至少,我玩过的魔兽争霸在网络同步方面做得非常好,即便是网络状况很不稳定,依然可以保证用户数据不会出现意想不 ...
- ios ASIHTTPRequest类库简介和使用说明
官方网站: http://allseeing-i.com/ASIHTTPRequest/ .可以从上面下载到最新源码,以及获取到相关的资料. 使用iOS SDK中的HTTP网络请求API,相当的复杂, ...
- Android Activity 半透明效果(Translucent)
本文转自:http://norety.javaeye.com/blog/648725 今天试着做activity半透明的效果,做出来之后才发现想复杂了!很简单的几句就可以实现,不多说了,贴代码! 1. ...
- oneThink发生错误,获取当前执行的SQL语句!
echo D('AnswerInfoView')->getLastSql();die();
- 熵 互信息 Gini指数 和 不纯度
在学习决策树类的算法时,总是绕不开 信息熵.Gini指数和它们相关联的概念,概念不清楚,就很难理解决策树的构造过程,现在把这些概念捋一捋. 信息熵 信息熵,简称熵,用来衡量随机变量的不确定性大小,熵越 ...