MySQL练习九:查找最晚入职员工的所有信息

卡二条的技术圈

共 1712字,需浏览 4分钟

 ·

2021-09-17 01:58

有一个员工employees表简况如下:

0BFB4D140D9C3E92AF681D9F9CB92D55

请你查找employees里最晚入职员工的所有信息,以上例子输出如下:

D2ABA1E2F5834850B16146F168AC5476

对应SQL语句:

  1. DDL
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`));
  1. insert 语句
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');

参考答案:

SELECT * FROM employees where  hire_date = (SELECT hire_date from employees  order by hire_date desc LIMIT 0, 1);

试题解析:

  1. 首先明白此题是查询最晚入职的员工,我们直接根据入职日期做一个倒序即可。

  2. 倒序得到最晚的入职日期,在到用户表去查询日期等于该日期即可。

  3. 此题很容易遗漏,同一天入职的员工不止一个,会存在多个情况。

相关试题:

  1. SQL练习八

  2. MySQL练习七

  3. MySQL练习六

  4. MySQL练习五


浏览 41
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报