select t2.dept_no, t2.emp_no, t1.salary from salaries as t1 join dept_manager as t2 on t1.emp_no=t2.emp_no where t1.to_date='9999-01-01' and t2.to_date='9999-01-01'
第10题
题目描述:获取所有非manager的员工emp_no
1 2 3 4 5 6 7 8 9 10 11 12 13 14
CREATE TABLE `dept_manager` ( `dept_no` char(4) NOT NULL, `emp_no` int(11) NOT NULL, `from_date` dateNOT NULL, `to_date` dateNOT NULL, PRIMARY KEY (`emp_no`,`dept_no`)); CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` dateNOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` char(1) NOT NULL, `hire_date` dateNOT NULL, PRIMARY KEY (`emp_no`));
输出示例:
emp_no
10001
10003
10007
10008
10009
10011
答案:
1 2 3 4 5 6
select t1.emp_no from employees as t1 leftjoin dept_manager as t2 on t1.emp_no=t2.emp_no where t2.dept_no isnull;
select t1.emp_no, t2.emp_no as manager_no from dept_emp as t1 leftjoin dept_manager as t2 on t1.dept_no=t2.dept_no where t1.to_date='9999-01-01' and t2.to_date='9999-01-01' and t1.emp_no<>t2.emp_no;
select t1.dept_no, t1.emp_no, t2.salary from dept_emp as t1 innerjoin salaries as t2 on t1.emp_no = t2.emp_no and t1.to_date ='9999-01-01' and t2.to_date ='9999-01-01' where t2.salary = ( selectmax(t3.salary) from dept_emp as t4 innerjoin salaries as t3 on t3.emp_no = t4.emp_no and t3.to_date ='9999-01-01' and t4.to_date ='9999-01-01' where t4.dept_no = t1.dept_no groupby t4.dept_no) orderby t1.dept_no;
CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` dateNOT NULL, `to_date` dateNOT NULL, PRIMARY KEY (`emp_no`,`from_date`)); CREATE TABLE IF NOTEXISTS "titles" ( `emp_no` int(11) NOT NULL, `title` varchar(50) NOT NULL, `from_date` dateNOT NULL, `to_date` dateDEFAULTNULL);
输出示例:
title
avg
Engineer
94409.0
Senior Engineer
69009.2
Senior Staff
91381.0
Staff
72527.0
答案:
1 2 3 4 5 6 7 8 9
select title, avg(salary) as avg from salaries as t1 join titles as t2 on t1.emp_no=t2.emp_no and t1.to_date='9999-01-01' and t2.to_date='9999-01-01' groupby t2.title;
select emp_no, salary from salaries where to_date='9999-01-01' and salary=( select salary from salaries where to_date='9999-01-01' groupby salary orderby salary desc limit 1,1 );
第18题
题目描述:查找当前薪水(to_date=’9999-01-01’)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by
1 2 3 4 5 6 7 8 9 10 11 12 13 14
CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` dateNOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` char(1) NOT NULL, `hire_date` dateNOT NULL, PRIMARY KEY (`emp_no`)); CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` dateNOT NULL, `to_date` dateNOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
输出示例:
emp_no
salary
last_name
first_name
10009
94409
Peac
Sumant
答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT t2.emp_no, t1.salary, t2.last_name, t2.first_name FROM salaries AS t1 JOIN employees AS t2 ON t1.emp_no=t2.emp_no AND t1.to_date='9999-01-01' WHERE ( SELECTCOUNT(DISTINCT t3.salary) FROM salaries AS t3 WHERE t3.to_date='9999-01-01' AND t3.salary>t1.salary )=1 groupby salary;
CREATE TABLE `departments` ( `dept_no` char(4) NOT NULL, `dept_name` varchar(40) NOT NULL, PRIMARY KEY (`dept_no`)); CREATE TABLE `dept_emp` ( `emp_no` int(11) NOT NULL, `dept_no` char(4) NOT NULL, `from_date` dateNOT NULL, `to_date` dateNOT NULL, PRIMARY KEY (`emp_no`,`dept_no`)); CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` dateNOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` char(1) NOT NULL, `hire_date` dateNOT NULL, PRIMARY KEY (`emp_no`));
输出示例:
last_name
first_name
dept_name
Facello
Georgi
Marketing
省略
省略
省略
Sluis
Mary
NULL
答案:
1 2 3 4 5 6 7 8 9
SELECT last_name, first_name, dept_name FROM employees AS t1 LEFTJOIN dept_emp AS t2 ON t1.emp_no=t2.emp_no LEFTJOIN departments AS t3 ON t2.dept_no=t3.dept_no;
SELECT emp_no, growth FROM(SELECT a.emp_no, b.salary-a.salary AS growth FROM (SELECT t1.emp_no, salary FROM employees AS t1, salaries AS t2 WHERE t1.emp_no=t2.emp_no AND t1.hire_date=t2.from_date) AS a, (SELECT t1.emp_no, salary FROM employees AS t1, salaries AS t2 WHERE t1.emp_no=t2.emp_no AND t2.to_date='9999-01-01') AS b WHERE a.emp_no=b.emp_no)AS c ORDERBY c.growth
SELECT emp_no, growth FROM(SELECT a.emp_no, b.salary-a.salary AS growth FROM (SELECT t1.emp_no, salary FROM employees AS t1, salaries AS t2 WHERE t1.emp_no=t2.emp_no AND t1.hire_date=t2.from_date) AS a, (SELECT t1.emp_no, salary FROM employees AS t1, salaries AS t2 WHERE t1.emp_no=t2.emp_no AND t2.to_date='9999-01-01') AS b WHERE a.emp_no=b.emp_no)AS c ORDERBY c.growth
输出示例:
dept_no
dept_name
sum
d001
Marketing
24
d002
Finance
14
d003
Human Resources
13
d004
Production
24
d005
Development
25
d006
Quality Management
25
答案:
1 2 3 4 5 6 7 8 9 10 11
select t1.dept_no, t3.dept_name, count(t2.salary) as sum from dept_emp as t1, salaries as t2, departments as t3 where t3.dept_no=t1.dept_no and t1.emp_no=t2.emp_no groupby t1.dept_no