首页 > 试题广场 >

查找最晚入职员工的所有信息

[编程题]查找最晚入职员工的所有信息
  • 热度指数:756209 时间限制: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
10004
1954-05-01 Christian  
Koblick   
M    
1986-12-01 
示例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');

输出

10008|1958-02-19|Saniya|Kalloufi|M|1994-09-15
select * from employees
where hire_date =
(select max(hire_date) from employees)
我认为这个是对的,其他top和limit方法有牵强之处,与给定数据集有关

最晚入职的当天未必就一个人,也许有多人,使用排序并限制得只能取得指定数量的结果

发表于 2017-07-11 14:40:15 回复(113)
SELECT * FROM employees ORDER BY hire_date DESC LIMIT 0,1;
SELECT * FROM employees WHERE hire_date = (SELECT MAX(hire_date) FROM employees);
LIMIT m,n : 表示从第m+1条开始,取n条数据;
LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。
本题limit 0,1 表示从第(0+1)条数据开始,取一条数据,即取出最晚入职员工。
编辑于 2017-07-10 09:31:37 回复(24)
最热衷的讨论就是,有多条相同hire_date的日期的人讨论。
首先如果用limit的话,坑定是错误的,因为相同hire_date哟很多条,你limit以后就只能显示你的规定的数目。我插入记录的时候插入反了,所以我用asc排序,所以这limit是不多的、
2:只有select * from employees where hire_date = (select max(hire_date) from employees);这样可以查找出等于最大的所有记录。
大家可以多插入几条记录来查看就知道了额。
发表于 2018-03-19 14:05:58 回复(16)
SELECT * FROM employees WHERE hire_date = (SELECT MAX(hire_date) FROM employees);
法一:子查询
先找出 hire_date 字段的最大值,再把该值当成  employees 表的 hire_date 查询条件。 

SELECT * FROM employees ORDER BY hire_date DESC LIMIT 0,1
法二:排序,降序。
hire_date字段排序降序,此时最晚的时间排在第一个,再用LIMIT取出。
发表于 2017-07-13 00:36:32 回复(3)
如果需要考虑最晚那一天有多个人入职的话,那么除了对hire_date进行逆序排序外,还需要对emp_no进行逆序排序,才能得到那个最晚的入职者。
发表于 2020-01-11 10:52:39 回复(8)
-- 如果最晚只有一个人或只需要选一个最晚的
select *
from employees
order by hire_date desc
limit 1

-- 如果最晚有多个人或者为了适应更多情况
select *
from employees
where hire_date = (
                      select max(hire_date) from employees
                  )

发表于 2021-07-20 14:10:51 回复(0)
用子查询语句,找出最晚入职的日期
select * from employees where hire_date in (select max(hire_date) from employees);

发表于 2017-09-14 16:40:48 回复(1)


select emp_no,
    birth_date,
    first_name,
    last_name,
    gender,
    max(hire_date)
from employees

..大佬们是想麻烦了吗。。。

发表于 2019-07-30 10:07:41 回复(12)
我觉得我的比较更容易理解吧。分享给大家
SELECT * FROM employees
ORDER BY hire_date DESC
LIMIT 1
发表于 2018-09-08 10:49:02 回复(8)
1>select * from employee where hire_date = (select max(hire_date) from employee);

2>select * from employee order by hire_date desc limit 1;

3>select * from employee order by hire_date desc limit 0,1;

发表于 2017-12-11 09:00:12 回复(0)
select * from employees where hire_date=(select max(hire_date) from employees);
发表于 2017-09-25 09:05:30 回复(1)
这个牛客测试环境是不是有点问题,我跟排行第一的代码一模一样(我还复制了一遍)但是就是不给我过。。。
发表于 2021-10-03 20:42:56 回复(3)
select * from employees order by hire_date desc limit 0,1
编辑于 2017-07-06 14:41:39 回复(0)

按时间降序排列,然后选择第一条数据,就是最新的日期。

select * from employees order by hire_date desc limit 0,1
发表于 2017-11-08 17:07:26 回复(9)
1、关键字:最新入职、所有信息
2、对应语句
select *
from employees
order by hire_date desc
limit 1
3、问题剖析:在题目假设员工入职的日期都不是同一天情况下,可以使用该语句;但当存在同一天入职的员工时,该语句便不正确了。可以使用如下语句
select * 
from employees
where hire_date = (select max(hire_date) 
    from employees);
发表于 2020-08-01 14:03:22 回复(1)
思路:分页方式 取根据hire_date,desc排序第一个数据
select * from employees order by hire_date desc limit 0,1

发表于 2017-08-06 12:52:43 回复(0)
select * from employees where hire_date = (select max(hire_date) from employees);
发表于 2017-07-07 09:48:04 回复(1)

【场景】:查找最大值的所有信息

【分类】:in子查询

分析思路

难点:

1.考虑到最晚入职的员工不止一名的情况。

员工表中的入职时间是只记录到天没有精确到秒,会有可能出现最晚入职的员工不止一名的情况。所以这也在启发我们建表的时候最好考虑到这种情况,把入职时间精确到秒。

如果有多名员工最晚入职如何查找

分两步走,一、先找到入职最晚(最大)日期;二、再根据日期筛选员工信息。

  • [使用]:in、= 子查询

扩展

前往查看:MySQL 嵌套子查询 with子句 from子查询 in子查询 join子查询

错误代码

对入职日期倒排序,取第一条数据。

#错误解法
select *
from employees
order by hire_date desc
limit 1

求解代码

方法一:

in 子查询

select 
    emp_no,
    birth_date,
    first_name,
    last_name,
    gender,
    hire_date
from employees
where hire_date in (
    select
        max(hire_date) as max_date
    from employees)

方法二:

= 子查询

select 
    emp_no,
    birth_date,
    first_name,
    last_name,
    gender,
    hire_date
from employees
where hire_date = (
    select
        max(hire_date) as max_date
    from employees)
发表于 2023-03-03 18:30:49 回复(0)
SELECT * from employees order by  hire_date  desc LIMIT 1
发表于 2022-02-10 10:52:56 回复(0)
SELECT *
FROM employees
WHERE hire_date = (
    SELECT MAX(hire_date)
    FROM employees
)
## 看到有其他朋友们用到了ORDER BY + LIMIT 0,1来实现
如下:
SELECT *
FROM employees
ORDER BY hire_date DESC
LIMIT 0,1
但考虑实际的话不一定入职最晚的只有一人,所以用聚合函数+where子查询更合适;
这里补充LIMIT的用法
LIMIT x,y
返回[x+1,y]范围内SELECT的数据
发表于 2022-01-30 18:46:09 回复(0)