225 lines
5.9 KiB
Plaintext
225 lines
5.9 KiB
Plaintext
[TOC]
|
||
|
||
# 查找最晚入职员工的所有信息
|
||
|
||
```sql
|
||
select *
|
||
from employees
|
||
order by hire_date desc
|
||
limit 1;
|
||
```
|
||
|
||
# 查找入职员工时间排名倒数第三的员工所有信息
|
||
|
||
```sql
|
||
select *
|
||
from employees
|
||
order by hire_date desc
|
||
limit 2, 1;
|
||
```
|
||
|
||
# 查找各个部门当前领导当前薪水详情以及其对应部门编号dept_no
|
||
|
||
```sql
|
||
select s.emp_no, s.salary, s.from_date, s.to_date, d.dept_no
|
||
from salaries as s inner join dept_manager as d
|
||
on d.emp_no = s.emp_no
|
||
and d.to_date = '9999-01-01'
|
||
and s.to_date = '9999-01-01';
|
||
```
|
||
|
||
# 查找所有已经分配部门的员工的last_name和first_name
|
||
|
||
```sql
|
||
select e.last_name, e.first_name, d.dept_no
|
||
from employees as e inner join dept_emp as d
|
||
on e.emp_no = d.emp_no;
|
||
```
|
||
|
||
# 查找所有员工的last_name和first_name以及对应部门编号dept_no
|
||
|
||
也包括展示没有分配具体部门的员工
|
||
|
||
```sql
|
||
select e.last_name, e.first_name, d.dept_no
|
||
from employees as e left outer join dept_emp as d
|
||
on e.emp_no = d.emp_no;
|
||
```
|
||
|
||
# 查找所有员工入职时候的薪水情况
|
||
|
||
```sql
|
||
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;
|
||
```
|
||
|
||
# 查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
|
||
|
||
```sql
|
||
select emp_no, count(*) as t
|
||
from salaries
|
||
group by emp_no
|
||
having t > 15;
|
||
```
|
||
|
||
# 找出所有员工当前具体的薪水salary情况
|
||
|
||
```sql
|
||
select distinct salary
|
||
from salaries
|
||
where to_date = '9999-01-01'
|
||
order by salary desc;
|
||
```
|
||
|
||
# 获取所有部门当前manager的当前薪水情况
|
||
|
||
```sql
|
||
select d.dept_no, d.emp_no, s.salary
|
||
from salaries as s inner join dept_manager as d
|
||
on d.emp_no = s.emp_no
|
||
and d.to_date = '9999-01-01'
|
||
and s.to_date = '9999-01-01';
|
||
```
|
||
|
||
# 获取所有非manager的员工emp_no
|
||
|
||
```sql
|
||
select emp_no
|
||
from employees
|
||
where emp_no not in (
|
||
select emp_no
|
||
from dept_manager
|
||
)
|
||
```
|
||
|
||
# 获取所有员工当前的manager
|
||
|
||
```sql
|
||
select d1.emp_no, d2.emp_no as manager_no
|
||
from dept_emp as d1 inner join dept_manager as d2
|
||
on d1.dept_no = d2.dept_no
|
||
and d1.to_date = '9999-01-01'
|
||
and d2.to_date = '9999-01-01'
|
||
and d1.emp_no <> d2.emp_no
|
||
```
|
||
|
||
# 获取所有部门中当前员工薪水最高的相关信息
|
||
|
||
```sql
|
||
select d.dept_no, d.emp_no, MAX(s.salary) as salary
|
||
from dept_emp as d inner join salaries as s
|
||
on 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
|
||
```
|
||
|
||
# 从titles表获取按照title进行分组
|
||
|
||
```sql
|
||
select title, COUNT(*) as t
|
||
from titles
|
||
group by title
|
||
having t >= 2
|
||
```
|
||
|
||
# 从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略。
|
||
|
||
```sql
|
||
select title, COUNT(distinct emp_no) as t
|
||
from titles
|
||
group by title
|
||
having t >= 2
|
||
```
|
||
|
||
# 查找employees表所有emp_no为奇数
|
||
|
||
```sql
|
||
select *
|
||
from employees
|
||
where emp_no % 2 = 1 and last_name != 'Mary'
|
||
order by hire_date desc
|
||
```
|
||
|
||
# 统计出当前各个title类型对应的员工当前薪水对应的平均工资
|
||
|
||
```sql
|
||
select t.title, AVG(s.salary) as avg
|
||
from titles as t inner join salaries as s
|
||
on t.emp_no = s.emp_no
|
||
and t.to_date = '9999-01-01'
|
||
and s.to_date = '9999-01-01'
|
||
group by t.title
|
||
```
|
||
|
||
# 获取当前薪水第二多的员工的emp_no以及其对应的薪水salary
|
||
|
||
```sql
|
||
select emp_no, salary
|
||
from salaries
|
||
order by salary desc
|
||
limit 1, 1
|
||
```
|
||
|
||
# 查找当前薪水排名第二多的员工编号emp_no
|
||
|
||
```sql
|
||
select e.emp_no, MAX(s.salary) as 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 not in (
|
||
select MAX(salary)
|
||
from salaries
|
||
where s.to_date = '9999-01-01'
|
||
)
|
||
```
|
||
|
||
# 查找所有员工的last_name和first_name以及对应的dept_name
|
||
|
||
查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
|
||
|
||
本题思路为运用两次LEFT JOIN连接嵌套
|
||
1、第一次LEFT JOIN连接employees表与dept_emp表,得到所有员工的last_name和first_name以及对应的dept_no,也包括暂时没有分配部门的员工
|
||
2、第二次LEFT JOIN连接上表与departments表,即连接dept_no与dept_name,得到所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
|
||
|
||
```sql
|
||
SELECT em.last_name, em.first_name, dp.dept_name
|
||
FROM (employees AS em LEFT JOIN dept_emp AS de ON em.emp_no = de.emp_no)
|
||
LEFT JOIN departments AS dp ON de.dept_no = dp.dept_no
|
||
```
|
||
|
||
# 查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth
|
||
|
||
```sql
|
||
SELECT (MAX(salary)-MIN(salary)) AS growth
|
||
FROM salaries WHERE emp_no = '10001'
|
||
```
|
||
|
||
# 查找所有员工自入职以来的薪水涨幅情况
|
||
|
||
```sql
|
||
select a.emp_no, (b.salary - c.salary) as growth
|
||
from
|
||
employees as a
|
||
inner join salaries as b
|
||
on a.emp_no = b.emp_no and b.to_date = '9999-01-01'
|
||
inner join salaries as c
|
||
on a.emp_no = c.emp_no and a.hire_date = c.from_date
|
||
order by growth asc
|
||
```
|
||
|
||
# 统计各个部门对应员工涨幅的次数总和
|
||
|
||
本题关键是要将 每个部门分组,并分别统计工资记录总数,思路如下:
|
||
1、用INNER JOIN连接dept_emp表和salaries表,并以dept_emp.no分组,统计每个部门所有员工工资的记录总数
|
||
2、再将上表用INNER JOIN连接departments表,限制条件为两表的dept_no相等,找到dept_no与dept_name的对应关系,最后依次输出dept_no、dept_name、sum
|
||
|
||
```sql
|
||
SELECT de.dept_no, dp.dept_name, COUNT(s.salary) AS sum
|
||
FROM (dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no)
|
||
INNER JOIN departments AS dp ON de.dept_no = dp.dept_no
|
||
GROUP BY de.dept_no
|
||
``` |