一、题目
描述
有一个员工employees表简况如下:
请你查找employees里入职员工时间排名倒数第三的员工所有信息,以上例子输出如下:
示例1
输入:
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
https://www.nowcoder.com/practice/ec1ca44c62c14ceb990c3c40def1ec6c?tpId=82&tags=&title=&difficulty=0&judgeStatus=0&rp=1
二、解题思路
1.出错
(1) DENSE_RANK() OVER(ORDER BY hire_date)
结果为1,1,2形式
#入职时间排名倒数第三
#窗口函数
SELECT *
FROM employees
WHERE hire_date=(
SELECT DENSE_RANK() OVER(ORDER BY hire_date) RANK_1
FROM employees
WHERE RANK_1=3
);
问题:不能识别rank_1
原因: sql语句执行顺序:
FROM ---> WHERE ---> GROUP BY ---> SELECT---->ORDER BY ----> LIMIT
当执行到WHERE时,并没有执行SELECT。
2.改进
SELECT emp_no,birth_date,first_name,last_name,gender,hire_date
FROM
(
SELECT *,DENSE_RANK() OVER(ORDER BY hire_date DESC) RANK_1
FROM employees
) as template
WHERE Rank_1=3;
3.WHERE子查询
SELECT emp_no,birth_date,first_name,last_name,gender,hire_date
FROM employees e1
WHERE
(
SELECT count(distinct hire_date)
FROM employees e2
WHERE e2.hire_date >e1.hire_date
)=2;