11.
emp_no
int(11) NOT NULL, title
varchar(50) NOT NULL, from_date
date NOT NULL, to_date
date DEFAULT NULL);select title,count(title) as t
from titles
group by title
having t>=2
2.
emp_no
int(11) NOT NULL, title
varchar(50) NOT NULL, from_date
date NOT NULL, to_date
date DEFAULT NULL);select title,count(title)as t
from (select distinct emp_no,title,from_date,to_date
from titles)as temple
group by title
having t>=2
13.
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, emp_no
));select *
from employees
where emp_no&1 and last_name not in(select last_name
from employees
where last_name='Marry')
order by hire_date desc
select * from employees
where emp_no % 2 = 1
and last_name != 'Mary'
order by hire_date desc
14.
salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
)); emp_no
int(11) NOT NULL, title
varchar(50) NOT NULL, from_date
date NOT NULL, to_date
date DEFAULT NULL);select titles.title,avg(salaries.salary)
from titles join salaries on salaries.emp_no=titles.emp_no
where salaries.to_date='9999-01-01'
and titles.to_date='9999-01-01'
group by titles.title
15.
salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
))select emp_no,salary
from salaries
where to_date='9999-01-01'
and salary in (select distinct salary
from salaries
order by salary desc
limit 1,1)
16.
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, emp_no
)); salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));SELECT e.emp_no, MAX(s.salary) AS salary, e.last_name, e.first_name
FROM employees AS e JOIN salaries AS s
ON e.emp_no = s.emp_no
WHERE s.to_date = '9999-01-01'
AND s.salary NOT IN (SELECT MAX(salary) FROM salaries WHERE to_date = '9999-01-01')
17.
departments
( dept_no
char(4) NOT NULL, dept_name
varchar(40) NOT NULL, dept_no
)); dept_emp
( emp_no
int(11) NOT NULL, dept_no
char(4) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); 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, emp_no
));select e.last_name,e.first_name,temple.dept_name
from employees e left join(select * from departments d,dept_emp where d.dept_no=dept_emp.dept_no)as temple
on e.emp_no=temple.emp_no
18.
salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));#考虑工资会比入职前低的情况
select((select salary from salaries
where emp_no=10001
order by to_date desc limit 0,1)-
(select salary from salaries
where emp_no=10001
order by from_date limit 0,1))
as growth
select (max(salary)-min(salary)) as growth
from salaries
where emp_no=10001
19.
题目描述
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
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 current_salary.emp_no,(current_salary.salary -hire_salary.salary) as growth
from ( employees join salaries on employees.emp_no = salaries.emp_no and salaries.to_date='9999-01-01') as current_salary
join ( employees join salaries on employees.emp_no = salaries.emp_no and salaries.from_date = employees.hire_date)as hire_salary
on current_salary.emp_no = hire_salary.emp_no
order by growth
20.
departments
( dept_no
char(4) NOT NULL, dept_name
varchar(40) NOT NULL, dept_no
)); dept_emp
( emp_no
int(11) NOT NULL, dept_no
char(4) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));SELECT de.dept_no, dp.dept_name, COUNT(s.salary) AS sum
FROM (dept_emp AS de JOIN salaries AS s ON de.emp_no = s.emp_no)
JOIN departments AS dp ON de.dept_no = dp.dept_no
GROUP BY de.dept_no