查找入职员工时间排名倒数第三的员工所有信息
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 IN(
SELECT DISTINCT hire_date FROM employees
ORDER BY hire_date DESC LIMIT 2,1
)
其中LIMIT 在sql server中不适用 但是题目的编译环境允许
这道题明确指明了 倒数第三 所以只要找出那个时间 再用where找出所有行即可。
这里要注意 时间类型是data的 存在重复(同一天入职) 所以要去掉重复的 用DISTINCT