牛客网数据库SQL实战

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

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`));

SELECT * FROM employees
WHERE hire_date = (SELECT max(hire_date) FROM 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`));

SELECT * FROM employees
WHERE hire_date = (SELECT hire_date FROM employees ORDER BY hire_date DESC LIMIT 2,1);
-- LIMIT 2,1表示从第2+1条记录开始,取1条记录

 

转载于:https://www.cnblogs.com/DianeSoHungry/p/8016338.html

你可能感兴趣的:(牛客网数据库SQL实战)