题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息

有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group by后面的属性,另一种是聚集函数。

2)在选取最大Salary时必须使用e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId两个条件,要不然会有重复。

基于这些考虑可以使用派生表查询来找出最大Salary,然后与Department表做自然连接。(最后的升序还是降序无所谓)

select dep.Name as Department, pans.Name as Employee,
pans.Salary as Salary
from Department dep, (
select e1.* from
Employee e1, (select DepartmentId, max(Salary) as Salary
from Employee group by DepartmentId) e2
where e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId
) pans
where dep.Id=pans.DepartmentId
order by pans.Salary desc;

LeetCode - Department Highest Salary的更多相关文章

  1. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  2. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  3. LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. LeetCode DB: Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  6. [LeetCode#184]Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  7. [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  8. [LeetCode] Nth Highest Salary 第N高薪水

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  9. [LeetCode] Second Highest Salary 第二高薪水

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

随机推荐

  1. 简单文件系统构建ramdisk

    1.  BusyBox编译工具,包含bin, sbin, usr,  linuxrc. 2.  添加相关重要目录:dev,  etc,  mnt, proc,  sys, lib, var, tmp. ...

  2. MongoDB自动删除过期数据--TTL索引

      前序: 由于公司业务需求,对于3个月前的过期数据需要进行删除动作,以释放空间和方便维护 本来想的是使用crontab写个脚本定时执行,但是看到Mongo本身就有自动删除过期数据的功能,所以还是用一 ...

  3. 常用PHP文件操作函数

    注:文件操作函数的行为受到 php.ini 中设置的影响. 当在 Unix 平台上规定路径时,正斜杠 (/) 用作目录分隔符.而在 Windows 平台上,正斜杠 (/) 和反斜杠 (\) 均可使用. ...

  4. DIV内滚动条滚动到指定位置

    相对浏览器,将指定div滚到到指定位置,其用法如下: $("html,body").animate({scrollTop: $(obj).offset().top},speed); ...

  5. Asp.Net MVC EasyUI DataGrid查询分页

    function doSearch() { //查询方法 var searchValue = $('#txtQueryTC001').textbox('getText'); $('#dgCMSTC') ...

  6. Ubuntu-Python2.7安装 scipy,numpy,matplotlib 和pip

    一. scipy,numpy,matplotlib sudo apt-get install python-scipy sudo apt-get install python-numpy sudo a ...

  7. ROS 教程之 vision : 用各种摄像头获取图像

    可能有很多人想在ROS下学习视觉,先用摄像头获取图像,再用opencv做相应算法处理,可是ROS下图像的采集可不像平常的read一下那么简单,需要借助外部package的使用.而摄像头即可以用笔记本自 ...

  8. TensorFlow基础笔记(11) max_pool2D函数

    # def max_pool2d(inputs, # kernel_size, # stride=2, # padding='VALID', # data_format=DATA_FORMAT_NHW ...

  9. adb调试功能

    参考: http://www.cnblogs.com/meil/archive/2012/05/24/2516055.html http://www.biemmeitalia.net/blog/and ...

  10. Android ListView使用BaseAdapter与ListView的优化 (转至 http://www.open-open.com/lib/view/open1339485728006.html)

    在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点.读者可 ...