首页 > 试题广场 >

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

[编程题]查找入职员工时间升序排名的情况下的倒数第三的员工所有信息
  • 热度指数: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
窗口排序函数dense_rank() over ()
select
    a.emp_no,
    a.birth_date,
    a.first_name,
    a.last_name,
    a.gender,
    a.hire_date
from
    (
        select
            emp_no,
            birth_date,
            first_name,
            last_name,
            gender,
            hire_date,
            dense_rank() over ( order by
                    hire_date desc
            ) as rk
        from
            employees
    ) as a
where
    a.rk = 3
order by  a.emp_no
发表于 2024-11-20 10:13:24 回复(0)
SELECT * FROM employees WHERE hire_date = ( SELECT hire_date FROM ( SELECT DISTINCT hire_date FROM employees ORDER BY hire_date ASC ) AS subquery ORDER BY hire_date DESC LIMIT 1 OFFSET 2 ) ORDER BY emp_no;
发表于 2024-11-17 17:23:03 回复(0)
排序问题:
(确定无重复)
select
    *
from
    employees
order by
    hire_date DESC
limit
    1
offset
    2
考虑重复情况,很明显的排列特点想到窗口排序denseDENSE_RANK()
SELECT
    a.emp_no,
    a.birth_date,
    a.first_name,
    a.last_name,
    a.gender,
    a.hire_date
FROM
    (
        SELECT
            emp_no,
            birth_date,
            first_name,
            last_name,
            gender,
            hire_date,
            DENSE_RANK() OVER (
                ORDER BY
                    hire_date DESC
            ) AS m
        FROM
            employees
    ) AS a
WHERE
    a.m = 3;
注意要写具体的查询字段,不然会报错
查找资料说是select列表中列的顺序敏感的系统报错

发表于 2024-10-05 14:22:08 回复(1)
select *
from employees
where hire_date = (
    select distinct hire_date
    from employees
    order by hire_date desc
    limit 1 offset 2
)
需要注意同一天入职的人有多个,比如今天入职3个,昨天入职2个,前天入职2个。需要先对入职日期进行去重,然后在取值limit 1 offset 2

发表于 2024-10-01 15:26:35 回复(0)
先有个大概思路
select * from employees
文本中告诉你的条件包括:取倒数、第三、会有同一时间入职的、一个员工
按照顺序:order by...desc../   123 (跳过12可以用offset)/ distinct / limit 1  
合在一起:select * from employees where hire_date = (select distinct hire_date from employees order by hire_date desc limit 1 offset 2);

发表于 2024-09-17 00:11:56 回复(0)
SELECT *
FROM employees
ORDER BY hire_date DESC
LIMIT 2,1;
发表于 2024-09-13 13:57:19 回复(0)
select * from employees
where emp_no in (
    select emp_no from (select emp_no from employees order by hire_date desc limit 2,1) t
)
LIMIT m,n : 表示从第m+1条开始,取n条数据;
LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。


发表于 2024-09-04 10:11:18 回复(0)
SELECT e.*
FROM employees e
WHERE (
    SELECT COUNT(DISTINCT hire_date)
    FROM employees
    WHERE hire_date > e.hire_date
) = 2;

发表于 2024-08-22 10:02:44 回复(0)
SELECT
t.emp_no,
t.birth_date,
t.first_name,
t.last_name,
t.gender,
t.hire_date
FROM
(SELECT
*,
DENSE_RANK() over(order by hire_date desc) rk
FROM
employees) t 
WHERE t.rk = 3

发表于 2024-08-13 14:00:29 回复(0)
发表于 2024-08-10 23:13:12 回复(0)
SELECT *
FROM employees
Where hire_date=(select
Distinct hire_date
From employees
Order by hire_date desc
Limit 2,1)
发表于 2024-08-01 15:52:31 回复(0)
select * from employees
where hire_date=(
    select distinct  hire_date from employees order by ire_date desc limit 2,1
);
发表于 2024-07-24 14:19:18 回复(0)
#1-窗口函数

select 
    emp_no
    ,birth_date
    ,first_name
    ,last_name
    ,gender
    ,hire_date
from (
    select 
    *
    ,dense_rank()over(order by hire_date desc) as rk
    from employees
) a
where a.rk=3;

#2-子查询

select *
from employees
where hire_date=(
    select distinct hire_date
    from employees
    order by hire_date desc
    limit 2,1
)

发表于 2024-07-17 16:45:19 回复(0)
##可能不止一条记录;可能倒数第一/二也有两条记录呢,必须要用DISTINCT
select *
from employees e
where e.hire_date = (
    select DISTINCT e2.hire_date
    from employees e2
    order by e2.hire_date desc
    limit 2,1
);
发表于 2024-07-15 16:24:05 回复(0)