[LeetCode]Sql系列
题目1
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
相关
两个字段in的使用
代码
# Write your MySQL query statement below
select Department.Name as Department, Employee.Name as Employee,Salary
from Employee join Department
on Employee.DepartmentId = Department.Id
where (DepartmentId,Salary) in
(
select DepartmentId,Max(Salary) as Salary
from Employee
group by DepartmentId
)
题目2
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
相关
limit beg,len 的使用
代码
# Write your MySQL query statement below
select (
select distinct Salary
from employee
order by Salary desc
limit 1,1
) as SecondHighestSalary
题目3
部门表 Department:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| revenue | int |
| month | varchar |
+---------------+---------+
(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。
编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。
查询结果格式如下面的示例所示:
Department 表:
+------+---------+-------+
| id | revenue | month |
+------+---------+-------+
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
+------+---------+-------+
查询得到的结果表:
+------+-------------+-------------+-------------+-----+-------------+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1 | 8000 | 7000 | 6000 | ... | null |
| 2 | 9000 | null | null | ... | null |
| 3 | null | 10000 | null | ... | null |
+------+-------------+-------------+-------------+-----+-------------+
注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。
相关
题型:把数据行变为字段
if(条件,true执行,false执行)
代码
# Write your MySQL query statement below
select
id,
max(if(month = 'Jan',revenue,null)) as Jan_Revenue,
max(if(month = 'Feb',revenue,null)) as Feb_Revenue,
max(if(month = 'Mar',revenue,null)) Mar_Revenue,
max(if(month = 'Apr',revenue,null)) Apr_Revenue,
max(if(month = 'May',revenue,null)) May_Revenue,
max(if(month = 'Jun',revenue,null)) Jun_Revenue,
max(if(month = 'Jul',revenue,null)) Jul_Revenue,
max(if(month = 'Aug',revenue,null)) Aug_Revenue,
max(if(month = 'Sep',revenue,null)) Sep_Revenue,
max(if(month = 'Oct',revenue,null)) Oct_Revenue,
max(if(month = 'Nov',revenue,null)) Nov_Revenue,
max(if(month = 'Dec',revenue,null)) Dec_Revenue
from Department
group by id
题目4
Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
解释:
IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。
相关
题型:分组前几
方法:自连接,找到比它高的distinct的数目。
题解
# Write your MySQL query statement below
select d.Name as Department, e1.Name as Employee, e1.Salary
from Employee e1 join Department d
on e1.DepartmentId = d.Id
where (
select count(distinct e2.Salary)
from Employee e2
where e2.Salary > e1.Salary
and e2.DepartmentId = e1.DepartmentId
) < 3
题目5
编写一个 SQL 查询来实现分数排名。
如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
相关
排第几,同题目4思路,找distinct比它高的。
此外,查询结果作为字段。
为区别关键字Rank 需要将别名'Rank'加引号
代码
# Write your MySQL query statement below
select s1.Score,(
select count(distinct s2.Score)
from Scores s2
where s2.Score >= s1.Score)as 'Rank'
from Scores s1
order by s1.Score DESC
[LeetCode]Sql系列的更多相关文章
- [LeetCode]Sql系列4
##题目1 626. 换座位 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id. 其中纵列的 id 是连续递增的 小美想改变相邻俩学生的座位. ...
- [Leetcode]Sql系列3
题目1 产品数据表: Products +---------------+---------+ | Column Name | Type | +---------------+---------+ | ...
- [LeetCode]Sql系列2
题目 1205. 每月交易II Transactions 记录表 +----------------+---------+ | Column Name | Type | +-------------- ...
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- LeetCode SQL题目(第一弹)
LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...
- Influx Sql系列教程九:query数据查询基本篇二
前面一篇介绍了influxdb中基本的查询操作,在结尾处提到了如果我们希望对查询的结果进行分组,排序,分页时,应该怎么操作,接下来我们看一下上面几个场景的支持 在开始本文之前,建议先阅读上篇博文: 1 ...
- Influx Sql系列教程八:query数据查询基本篇
前面几篇介绍了InfluxDB的添加,删除修改数据,接下来进入查询篇,掌握一定的SQL知识对于理解本篇博文有更好的帮助,下面在介绍查询的基础操作的同时,也会给出InfluxSql与SQL之间的一些差别 ...
- Influx Sql系列教程七:delete 删除数据
前面介绍了使用insert实现新增和修改记录的使用姿势,接下来我们看一下另外一个简单的使用方式,如何删除数据 1. delete 语句 delete的官方语法如下 DELETE FROM <me ...
- Influx Sql系列教程六:insert 修改数据
在influxdb中没有专门的修改数据的update语句,对于influxdb而言,如果想修改数据,还是得使用我们前面的说到的insert来实现,那么怎么判断一条insert语句是插入还是修改呢? 1 ...
随机推荐
- [netty4][netty-transport]netty之nio传输层
[netty4][netty-transport]netty之nio传输层 nio基本处理逻辑 查看这里 Selector的处理 Selector实例构建 NioEventLoop.openSelec ...
- 日记——递归or搜索?
好久没发博了,今天发一篇. 这两天学校功课比较紧,编程稍微放了放做题量. 抽空学了学深搜,感谢zah同学给我讲解dfs,浅显易懂,我很快就适应了. 做了几个基础题,没有想象中那么难(菜鸡BB,因为题简 ...
- Ubuntu操作系统(文件传输)
首先选择Ubuntu版本为偶数版本--(系统比较稳定软件源比较齐全) Ubuntu和windows之间的文件传输首先在Windows上安装连接工具winscp 在Ubuntu开启SSH服务(https ...
- Ncdu强大的磁盘查看命令
简介 项目地址: https://dev.yorhel.nl/ncdu Ncdu (NCurses Disk Usage) 是一个基于 Ncurses 库的 du 命令的界面.它通过大家熟知的 du ...
- SCAFFOLD: Stochastic Controlled Averaging for On-Device Federated Learning
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 以下是对本文关键部分的摘抄翻译,详情请参见原文. arXiv:1910.06378v1 [cs.LG] 14 Oct 2019 Abstr ...
- python字典的概念与基本操作
字典是非常常用的一种数据结构,它与json格式的数据非常相似,核心就是以键值对的形式存储数据,关于Python中的字典做如下四点说明: 1.构造字典对象需要用大括号表示 {},每个字典元素都是以键值对 ...
- Java动态代理(二)——jdk动态代理
一.什么是动态代理?代理类在程序运行时创建的代理方式被成为动态代理.动态代理的代理类并不是在Java代码中定义的,而是在运行时根据我们在Java代码中的“指示”动态生成的.相比于静态代理, 动态代理的 ...
- shell 三剑客之 sed
sed 在shell 编程里也很常用,功能强大! 同grep一样,sed提供两种方式: 方式一:stdout | sed [option] "pattern command" 从文 ...
- EventLoop-浏览器篇2
最近又碰到了event loop问题,之前研究的实在是浅显(https://www.cnblogs.com/zx0423/p/12641637.html)所以今天主要讲述promise的链式调用,as ...
- [NOI2020]美食家 题解
题意分析 给出一个带权有向图,要求从节点 $1$ 出发,经过恰好 $T$ 的边权和,回到节点 $1$ ,求可经过的最大点权和.特别地,经过的边权和达到部分特殊数时,会有某个点的点权发生改变. 思路分析 ...