题目大概的意思是选出每个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. Ubuntu 下查看中文man手册方法

    转载自:http://blog.chinaunix.net/uid-24830506-id-3266493.html Ubuntu 中文man手册安装方法 分类: LINUX Ubuntu 下查看中文 ...

  2. php7+apache2.4 安装(window)

    一.下载 需要下载 php7.apache2.4.vc2015(VC库) PHP7下载地址:http://windows.php.net/download/ Apache 下载地址:https://h ...

  3. C++实现八皇后问题

    C++实现八皇后问题 #include <iostream> using std::cout; using std::endl; #include <iomanip> usin ...

  4. pdo mysql连接时报[2002] No such file or directory

    将PDO连接中的dsn的host由“localhost”改为“127.0.0.1”即可

  5. Windows与Linux下进程间通信技术比较

    一般我们写的程序都是以单个进程的方式来运行的,比较少涉及到多进程.特别是在windows下,因为Windows是按照线程来分配CPU时间片的,线程是最小的调度单位,所以在Windows下更多的用到多线 ...

  6. Apache2.4.x版wampserver本地php服务器如何让外网访问及启用.htaccess

    http://www.jb51.net/article/61193.htm ———————————————————————————————————————————— 这篇文章主要介绍了Apache2. ...

  7. 下载安装 Apache(Windows 64位)

    32位的Apache的下载安装:http://jingyan.baidu.com/album/2f9b480dae458f41cb6cc2ce.html?picindex=2 64位的Apache的下 ...

  8. 纯CSS实现圆角、可拖动的一个DIV模块层

    <style>body{ margin:0px; padding:0px; font-size:14px;}#t { position:absolute; float:left; left ...

  9. js学习系列之-----apply和call

    apply call 从字面意思就看出来,申请请,呼叫. 打个比方就是别人有什么功能,你向别人,申请 呼叫 一下,哥们拿你的功能用一下,而apply 和call就是实现这样的功能 apply 和cal ...

  10. Kafka Tools

    参考, https://cwiki.apache.org/confluence/display/KAFKA/System+Tools https://cwiki.apache.org/confluen ...