首页 > 试题广场 >

查找入职员工时间升序排名的情况下的倒数第三的员工所有信息

[编程题]查找入职员工时间升序排名的情况下的倒数第三的员工所有信息
  • 热度指数:861948 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个员工employees表简况如下:
emp_no
birth_date
first_name
last_name
gender hire_date
10001
1953-09-02
Georgi
Facello
M 1986-06-26
10002
1964-06-02
Bezalel
Simmel
F 1985-11-21
10003
1959-12-03
Parto
Bamford
M 1986-08-28
10004
1954-05-01
Christian
Koblick
M 1986-12-01

请你查找employees里入职员工时间升序排名的情况下倒数第三的员工所有信息,以上例子输出如下:
emp_no
birth_date
first_name
last_name
gender
hire_date
10001
1953-09-02
Georgi
Facello
M 1986-06-26
注意:可能会存在同一个日期入职的员工,所以入职员工时间排名倒数第三的员工可能不止一个,存在多个员工的情况按照emp_no升序排列
示例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');

输出

emp_no|birth_date|first_name|last_name|gender|hire_date
10005|1955-01-21|Kyoichi|Maliniak|M|1989-09-12
select *from employees order by hire_date limit 2,1;

发表于 2021-06-11 23:44:04 回复(0)
一条sql就包含很多知识点 评论对小白理解也不错

发表于 2021-05-22 11:41:11 回复(0)
#select * from employees order by hire_date desc limit 2,1
#distinct:去重
#select * from employees where hire_date = (select distinct hire_date from employees order by hire_date desc limit 2,1)
#group by:按字段分组,可实现去重
select * from employees where hire_date = (select hire_date from employees group by hire_date order by hire_date desc limit 2,1)
发表于 2021-04-25 20:04:29 回复(0)
LIMIT m,n : 表示从第m+1条开始,取n条数据;
LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。
select * 
from employees 
where 
    hire_date = 
    (
        select distinct hire_date
        from employees
        order by hire_date desc 
        limit 2,1
    )
依题意得:按员工的入职时间进行排序,取倒数第三的时间,只要入职时间等于这个时间的员工都符合题意
还有一种解法
select * 
from employees 
where 
    hire_date = 
    (
        select hire_date
        from employees
        group by hire_date
        order by hire_date desc 
        limit 2,1
    )
去重和分组的效果一样,但是distinct比group by语句更快,内存占用更小
发表于 2021-01-09 11:32:09 回复(1)
不去重(假设时间不重复):
select * 
from employees order by hire_date desc limit 2,1
去重:
select * from employees where hire_date =(select distinct hire_date from employees order by hire_date desc limit 2,1)


发表于 2019-07-17 13:16:03 回复(0)
select * from employees 
where hire_date = 
    (select distinct hire_date from employees order by hire_date desc limit 2,1);
为了去除重复,需要一个子查询去重。
发表于 2018-12-24 01:31:08 回复(0)