Mysql练习题13- 员工薪水中位数
需求:请编写SQL查询来查找每个公司的薪水中位数。挑战点:你是否可以在不使用任何内置的SQL函数的情况下解决此问题。
展示效果:
Id | Company | Salary |
---|---|---|
5 | A | 451 |
6 | A | 513 |
12 | B | 234 |
9 | B | 1154 |
14 | C | 2645 |
1 Create table If Not Exists Employee (Id int, Company varchar(255), Salary int);2 3 insert into Employee (Id, Company, Salary) values (1, 'A', 2341);4 insert into Employee (Id, Company, Salary) values (2, 'A', 341);5 insert into Employee (Id, Company, Salary) values (3, 'A', 15);6 insert into Employee (Id, Company, Salary) values (4, 'A', 15314);7 insert into Employee (Id, Company, Salary) values (5, 'A', 451);8 insert into Employee (Id, Company, Salary) values (6, 'A', 513);9 insert into Employee (Id, Company, Salary) values (7, 'B', 15); 10 insert into Employee (Id, Company, Salary) values (8, 'B', 13); 11 insert into Employee (Id, Company, Salary) values (9, 'B', 1154); 12 insert into Employee (Id, Company, Salary) values (10, 'B', 1345); 13 insert into Employee (Id, Company, Salary) values (11, 'B', 1221); 14 insert into Employee (Id, Company, Salary) values (12, 'B', 234); 15 insert into Employee (Id, Company, Salary) values (13, 'C', 2345); 16 insert into Employee (Id, Company, Salary) values (14, 'C', 2645); 17 insert into Employee (Id, Company, Salary) values (15, 'C', 2645); 18 insert into Employee (Id, Company, Salary) values (16, 'C', 2652); 19 insert into Employee (Id, Company, Salary) values (17, 'C', 65);
最终SQL:
1 select 2 b.id,3 b.company,4 b.salary5 from 6 (select7 id,8 company,9 salary, 10 case @com when company then @rk:=@rk+1 else @rk:=1 end rk, 11 @com:=company 12 from 13 employee, 14 (select @rk:=0, @com:='') a 15 order by 16 company,salary 17 ) b 18 left join 19 (select 20 company, 21 count(1)/2 cnt 22 from 23 employee 24 group by company 25 ) c 26 on 27 b.company=c.company 28 where 29 b.rk in (cnt+0.5,cnt+1,cnt);
Mysql练习题13- 员工薪水中位数相关推荐
- 【LeetCode-SQL】569. 员工薪水中位数
目录 一.题目 二.解决 1.窗口函数 2.自关联后分组筛选 三.参考 一.题目 表: Employee +--------------+---------+ | Column Name | Type ...
- LeetCode MySQL 569. 员工薪水中位数(over窗口函数)
文章目录 1. 题目 2. 解题 1. 题目 Employee 表包含所有员工.Employee 表有三列:员工Id,公司名和薪水. +-----+------------+--------+ |Id ...
- 【每日SQL打卡】DAY 4丨员工薪水中位数【难度困难】
活动介绍: 「数据仓库技术交流群」已经正式启动每日SQL打卡,帮助大家扎实基础,努力工作之余,别忘了自我提升. 欢迎报名和邀请小伙伴参与,一个人可能走得很快,但一群人会走得很远.
- 台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
「极客头条」-- 技术人员的新闻圈! CSDN 的读者朋友们早上好哇,「极客头条」来啦,快来看今天都有哪些值得我们技术人关注的重要新闻吧. 整理 | 梦依丹 出品 | CSDN(ID:CSDNnews ...
- 台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
「极客头条」-- 技术人员的新闻圈! CSDN 的读者朋友们早上好哇,「极客头条」来啦,快来看今天都有哪些值得我们技术人关注的重要新闻吧. 整理 | 梦依丹 出品 | CSDN(ID:CSDNnews ...
- 数据库系统原理与应用教程(073)—— MySQL 练习题:操作题 131-140(十七):综合练习
数据库系统原理与应用教程(073)-- MySQL 练习题:操作题 131-140(十七):综合练习 131.子查询(1) 该题目使用的表和数据如下: /* drop table if exists ...
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
数据库系统原理与应用教程(058)-- MySQL 练习题(二):单选题 1.在成绩表中查询绩点在 3.7-4.0 之间,并且挂科数为 0 的学生的所有信息.下列SQL语句不正确的是(D). A.SE ...
- python条件表达式三门课至少有一门及格_Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 2.查询"生物"课程比"物理"课程成绩高的所有学生的学号 1 SELECT2 * 3 FROM4 (5 SELECT6 ...
- java swing mysql实现的员工工资管理系统项目
下载链接https://download.csdn.net/download/RONNIE_Zz/12675313 项目功能 随着经济的发展,企业正向着大型化.规模化发展,而对于大中型企业,员工.职称 ...
最新文章
- MySQL 中的反斜杠 \\,真是太坑了!!
- 《A Berkeley View of systems challenges for AI》总结
- 编写程序处理一个 string 类型的 list 容器。在该容器9.27:中寻找一个特殊值,如果找到,则将它删除掉。用 deque容器重写上述程序。
- ConcurrentDictionary的ToDictionary
- Java基础——深入理解ReentrantLock
- python 类的内置方法_【转】[python] 类常用的内置方法
- 【Elasticsearch】为什么我在Elasticsearch集群中看到大量的拒绝?
- 基于windows server 2008 web服务器的性能,Windows Server 2008之应用程序服务器解析
- 在DBGrid中实现Copy、Paste功能 - DELPHI
- oracle rac单节点恢复,如何Oracle_RAC恢复一个节点总结
- 无任何网络提供程序接受指定的网络路径解决方法
- Charles最新版破解注册方法
- 推荐系统中的前沿技术研究与落地:深度学习、AutoML与强化学习 | AI ProCon 2019
- 路由器配置双宽带,并相互备份使用
- c语言字符结构数组初始化,C语言结构体数组初始化问题
- mac下如何将adobe acrobat reader dc设为.pdf的默认打开方式?
- 搭建MineCraft私服
- 怎么解决在微信中不能直接下载APP(APK)的方案
- APP自动化测试框架-UiAutomator2基础入门
- 201912一种改进动物音频分类的数据增强方法
热门文章
- omnidb数据库web管理工具安装
- 《Windows Vista for Developers》系列
- 运维的shell小编(1)
- 你应该将应用迁移到Spring 4的五个原因
- Jenkins + OSChina CI 最简
- ScaleIO 1.32现在可以免费下载安装使用了(除生产环境之外)
- 答读者问(27):一个大一学生有关未来规划的疑问及答复
- JRBeanCollectionDataSource cannot be resolved to a type 的原因
- Java 接受reactjs数据_[Java教程]react.js 父子组件数据绑定实时通讯
- verilog中的三目运算符