启航!
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)查找满足最后入职的(hire_date)条件的员工。可以使用的查询方法如下:
答案
1>select * from employees where hire_date = (select max(hire_date) from employees);
2>select * from employees order by hire_date desc limit 1;
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`));
思路
按照hire_date进行降序排列,利用limit输出第三行数据。(limit 2,1 表示取从第2行开始的第1行数据)
答案
select * from employees order by hire_date desc limit 2,1;
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
答案
select salaries.*,dept_manager.dept_no from salaries, dept_manager
where salaries.emp_no = dept_manager.emp_no
and salaries.to_date = "9999-01-01"
and dept_manager.to_date = "9999-01-01";
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
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 last_name,first_name,dept_no from employees,dept_emp
where dept_emp.emp_no=employees.emp_no;
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
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`));
思路
左外连接查询——left join
select * from dept left join emp on dept.id=emp.dept_id;
在内连接的基础上+左边表有而右边表没有的记录
(适应场景——就是左边表有的部门,右边表中并不存在该部门的员工,但是部门是存在的,因此不能丢弃)
答案
SELECT last_name,first_name,dept_no
from employees as e
left join
dept_emp as d
on e.emp_no=d.emp_no
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`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
答案
SELECT e.emp_no,s.salary
FROM employees as e
INNER JOIN salaries as s
ON e.emp_no=s.emp_no and e.hire_date=s.from_date
ORDER BY e.emp_no DESC;
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
参考知识点
group by、having
答案
select emp_no,count(emp_no) as t
from salaries
group by emp_no
having t>=15
未完待续