博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 数据库题解
阅读量:5052 次
发布时间:2019-06-12

本文共 2858 字,大约阅读时间需要 9 分钟。

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  |+------------+----------+--------+
View Code

解法:

1 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.
View Code

解法: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.

1 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) |+---------+------------+------------------+|       1 | 2015-01-01 |               10 ||       2 | 2015-01-02 |               25 ||       3 | 2015-01-03 |               20 ||       4 | 2015-01-04 |               30 |+---------+------------+------------------+For example, return the following Ids for the above Weather table:+----+| Id |+----+|  2 ||  4 |+----+Subscribe to see which companies asked this question.
View Code

解法:

1 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;

 

转载于:https://www.cnblogs.com/fisherinbox/p/6716600.html

你可能感兴趣的文章
单点登录技术
查看>>
使用LINQ取得已选中的CheckBox .
查看>>
感谢青春
查看>>
Jquery Uploadify4.2 falsh 实现上传
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>
数据结构3——浅谈zkw线段树
查看>>
通过ant调用shell脚本执行adb命令
查看>>
APP接口自动化测试JAVA+TestNG(一)之框架环境搭建
查看>>
php底层--1
查看>>
Servlet生命周期引起的问题
查看>>
关于gulp入门之图片压缩
查看>>
ZOJ 2136 Longest Ordered Subsequence
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
shell判断网络主机存活
查看>>
根据时间戳,增量同步数据的解决办法
查看>>
03 SeekBar 音频播放拖拽进度条
查看>>
自定义view实现阻尼效果的加载动画
查看>>