11、获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'。
结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_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 `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`));
select e.emp_no,m.emp_no as manager_no
from dept_manager m,dept_emp e
where e.dept_no = m.dept_no
and e.emp_no != m.emp_no
and m.to_date='9999-01-01';
讨论:https://www.nowcoder.com/questionTerminal/e50d92b8673a440ebdf3a517b5b37d62
12、获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
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 `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 dept_emp.dept_no,salaries.emp_no,max(salaries.salary)
from dept_emp
join salaries
on dept_emp.emp_no = salaries.emp_no
where dept_emp.to_date = '9999-01-01'
and salaries.to_date = '9999-01-01'
group by dept_emp.dept_no;
讨论:https://www.nowcoder.com/questionTerminal/4a052e3e1df5435880d4353eb18a91c6
13、从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
CREATE TABLE IF NOT EXISTS "titles" (
`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;
讨论:https://www.nowcoder.com/questionTerminal/72ca694734294dc78f513e147da7821e
14、从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略。
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
select title,count(DISTINCT emp_no) as t
from titles
group by title
having t >= 2;
讨论:https://www.nowcoder.com/questionTerminal/c59b452f420c47f48d9c86d69efdff20
15、查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列
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 last_name != 'Mary'
and emp_no %2 != 0
order by hire_date desc;
讨论:https://www.nowcoder.com/questionTerminal/a32669eb1d1740e785f105fa22741d5c