牛客網—資料庫SQL實戰(2)
11. 獲取所有員工當前的manager
獲取所有員工當前的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`));
輸入描述:
無
輸出描述:
emp_no | manager_no |
---|---|
10001 | 10002 |
10003 | 10004 |
10009 | 10010 |
select d1.emp_no, d2.emp_no as manager_no
from dept_emp as d1, dept_manager as d2
where d1.dept_no = d2.dept_no
and d1.emp_no <> d2.emp_no
and d1.to_date = '9999-01-01'
and d2.to_date = '9999-01-01'
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`));
輸入描述:
無
輸出描述:
dept_no | emp_no | salary |
---|---|---|
d001 | 10001 | 88958 |
d002 | 10006 | 43311 |
d003 | 10005 | 94692 |
d004 | 10004 | 74057 |
d005 | 10007 | 88070 |
d006 | 10009 | 95409 |
select d.dept_no, d.emp_no, max(s.salary) as salary
from dept_emp as d, salaries as s
where d.emp_no = s.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
group by d.dept_no
13. 從titles表獲取按照title進行分組
題目描述
從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);
輸入描述:
無
輸出描述:
title | t |
---|---|
Assistant Engineer | 2 |
Engineer | 4 |
省略 | 省略 |
Staff | 3 |
select title, count(title) as t
from titles
group by title
having t >= 2
14. 從titles表獲取按照title進行分組,注意對於重複的emp_no進行忽略
題目描述
從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);
輸入描述:
無
輸出描述:
title | t |
---|---|
Assistant Engineer | 2 |
Engineer | 3 |
省略 | 省略 |
Staff | 3 |
select title, count(distinct emp_no) as t
from titles
group by title
having t >= 2
15. 查詢employees表
題目描述
查詢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`));
輸入描述:
無
輸出描述:
emp_no | birth_date | first_name | last_name | gender | hire_date |
---|---|---|---|---|---|
10011 | 1953-11-07 | Mary | Sluis | F | 1990-01-22 |
10005 | 1955-01-21 | Kyoichi | Maliniak | M | 1989-09-12 |
10007 | 1957-05-23 | Tzvetan | Zielinski | F | 1989-02-10 |
10003 | 1959-12-03 | Parto | Bamford | M | 1986-08-28 |
10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 |
10009 | 1952-04-19 | Sumant | Peac | F | 1985-02-18 |
select emp_no, birth_date, first_name, last_name, gender, hire_date
from employees
where emp_no % 2 = 1
and last_name <> 'Mary'
order by hire_date desc
16. 統計出當前各個title型別對應的員工當前薪水對應的平均工資
題目描述
統計出當前各個title型別對應的員工當前薪水對應的平均工資。結果給出title以及平均工資avg。 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`)); 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);
輸入描述:
無
輸出描述:
title | avg |
---|---|
Engineer | 94409.0 |
Senior Engineer | 69009.2 |
Senior Staff | 91381.0 |
Staff | 72527.0 |
select t.title, avg(s.salary) as avg
from titles as t, salaries as s
where t.emp_no = s.emp_no
and t.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
group by t.title
17. 獲取當前薪水第二多的員工的emp_no以及其對應的薪水salary
題目描述
獲取當前(to_date='9999-01-01')薪水第二多的員工的emp_no以及其對應的薪水salary 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`));
輸入描述:
無
輸出描述:
emp_no | salary |
---|---|
10009 | 94409 |
select emp_no, salary
from salaries
order by salary desc limit 1,1
18. 查詢當前薪水排名第二多的員工編號emp_no以及其對應的薪水salary,不準使用order_by
題目描述
查詢當前薪水(to_date='9999-01-01')排名第二多的員工編號emp_no、薪水salary、last_name以及first_name,不準使用order by 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`));
輸入描述:
無
輸出描述:
emp_no | salary | last_name | first_name |
---|---|---|---|
10009 | 94409 | Peac | Sumant |
select e.emp_no, s.salary, e.last_name, e.first_name
from employees as e, salaries as s
where e.emp_no = s.emp_no
and s.to_date = '9999-01-01'
and s.salary = (select max(salary)
from salaries
where to_date = "9999-01-01"
and salary != (select max(salary)
from salaries
where to_date = "9999-01-01"))
19. 查詢所有員工的last_name和first_name以及對應的dept_name
題目描述
查詢所有員工的last_name和first_name以及對應的dept_name,也包括暫時沒有分配部門的員工 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` 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`));
輸入描述:
無
輸出描述:
last_name | first_name | dept_name |
---|---|---|
Facello | Georgi | Marketing |
省略 | 省略 | 省略 |
Sluis | Mary | NULL |
select e.last_name, e.first_name, d2.dept_name
from employees as e left join dept_emp as d1
on e.emp_no = d1.emp_no left join departments as d2
on d1.dept_no = d2.dept_no
20. 查詢員工編號emp_no為10001其自入職以來的薪水salary漲幅值growth
題目描述
查詢員工編號emp_no為10001其自入職以來的薪水salary漲幅值growth 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`));
輸入描述:
無
輸出描述:
growth |
---|
28841 |
select (
(select salary from salaries where emp_no = 10001 order by to_date desc limit 1) -
(select salary from salaries where emp_no = 10001 order by to_date asc limit 1)
) as growth