描述:有一个员工employees表简况如下:
emp_no | birth_date | first_name | last_name | gender | hire_date |
10001 | 1953/9/2 | Georgi | Facello | M | 1986/6/26 |
10002 | 1964/6/2 | Bezalel | Simmel | F | 1985/11/21 |
10003 | 1959/12/3 | Parto | Bamford | M | 1986/8/28 |
10004 | 1954/5/1 | Christian | Koblick | M | 1986/12/1 |
请你查找employees里入职员工时间排名倒数第三的员工所有信息,以上例子输出如下:
emp_no | birth_date | first_name | last_name | gender | hire_date |
10001 | 1953/9/2 | Georgi | Facello | M | 1986/6/26 |
注意:可能会存在同一个日期入职的员工,所以入职员工时间排名倒数第三的员工可能不止一个。
示例:
建表语句等 输入: drop table if exists `employees` ; CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` char(1) NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)); INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26'); INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21'); INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28'); INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01'); INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12'); INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02'); INSERT INTO employees VALUES(10007,'1957-05-23','Tzvetan','Zielinski','F','1989-02-10'); INSERT INTO employees VALUES(10008,'1958-02-19','Saniya','Kalloufi','M','1994-09-15'); INSERT INTO employees VALUES(10009,'1952-04-19','Sumant','Peac','F','1985-02-18'); INSERT INTO employees VALUES(10010,'1963-06-01','Duangkaew','Piveteau','F','1989-08-24'); INSERT INTO employees VALUES(10011,'1953-11-07','Mary','Sluis','F','1990-01-22'); 输出: 10005|1955-01-21|Kyoichi|Maliniak|M|1989-09-12
解题思路:
和第一题一样,只是说他找的是倒数第三个入职的员工 思路一:降序排列找第三个(第一个bug是不是就显示出来了) 所以只能用方法二 思路二:一样是是打行号(但是用哪个函数呢??是不是DENSE_RANK)
思路一:降序排列找第三个(第一个bug是不是就显示出来了) 所以只能用方法二
-- 先筛选出来倒数第三入职的日期 SELECT DISTINCT hire_date FROM employees ORDER BY hire_date DESC LIMIT 1 OFFSET 2 -- 子查询找到所有员工倒数第三的详细信息并返回 SELECT * FROM employees WHERE hire_date = ( SELECT DISTINCT hire_date FROM employees ORDER BY hire_date DESC LIMIT 1 OFFSET 2 );
思路二:一样是是大行好(但是用哪个函数呢??是不是DENSE_RANK)
-- 先打行号 select *, DENSE_RANK() over(order by hire_date desc )as n from employees -- 再筛选出倒数第三入职的 select emp_no,birth_date,first_name,last_name,gender,hire_date from( select *, DENSE_RANK() over(order by hire_date desc )as num from employees ) a where num=3
描述:有一个全部员工的薪水表salaries简况如下:
emp_no | salary | from_date | to_date |
10001 | 88958 | 2002/6/22 | 9999/1/1 |
10002 | 72527 | 2001/8/2 | 9999/1/1 |
10003 | 43311 | 2001/12/1 | 9999/1/1 |
有一个各个部门的领导表dept_manager简况如下:
dept_no | emp_no | to_date |
d001 | 10001 | 9999/1/1 |
d002 | 10003 | 9999/1/1 |
请你查找各个部门当前领导的薪水详情以及其对应部门编号dept_no,输出结果以salaries.emp_no升序排序,并且请注意输出结果里面dept_no列是最后一列,以上例子输出如下:
emp_no | salary | from_date | to_date | dept_no |
10001 | 88958 | 2002/6/22 | 9999/1/1 | d001 |
10003 | 43311 | 2001/12/1 | 9999/1/1 | d002 |
示例:
drop table if exists `salaries` ; drop table if exists `dept_manager` ; CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`)); CREATE TABLE `dept_manager` ( `dept_no` char(4) NOT NULL, `emp_no` int(11) NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`dept_no`)); INSERT INTO dept_manager VALUES('d001',10002,'9999-01-01'); INSERT INTO dept_manager VALUES('d002',10006,'9999-01-01'); INSERT INTO dept_manager VALUES('d003',10005,'9999-01-01'); INSERT INTO dept_manager VALUES('d004',10004,'9999-01-01'); INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01'); INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01'); INSERT INTO salaries VALUES(10004,74057,'2001-11-27','9999-01-01'); INSERT INTO salaries VALUES(10005,94692,'2001-09-09','9999-01-01'); INSERT INTO salaries VALUES(10006,43311,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10007,88070,'2002-02-07','9999-01-01');
思路分析:
找领导的薪水,需要做的是排除不是领导的员工。所以我们现在的主表应该是领导表,然后去关联薪水表 关联的条件是员工号相等 最后要求是按照员工号的升序排列
解题sql:
select dm.emp_no,s.salary,s.from_date,dm.to_date,dm.dept_no from dept_manager as dm left join salaries as s on dm.emp_no = s.emp_no WHERE dm.to_date='9999-01-01' -- 为什么要加这个判断呢?题目中要求是当前领导,表中如果存在辞职的领导就不符合题意了 AND s.to_date='9999-01-01' order by dm.emp_no asc -- asc 是默认的,可写可不写