1.查找最晚入职员工的所有信息
第一次代码:
select
*
from
employees
order by
hire_date desc limit 1;
以 hire_date 为降序排列(desc) 只显示第一条信息(limit 1).
因最晚入职的当天未必就一个人,也许有多人,使用排序并限制得只能取得指定数量的结果
第二次代码:
select
*
from
employees
where
hire_date = (select
max(hire_date)
from
employees);
同时占用内存少
2.查找入职员工时间排名倒数第三的员工所有信息
select
*
from
employees
order by
hire_date desc limit 2,1;
limit 参数,第一个参数:从哪儿开始查 ; 第二个参数:查几条
3.查找当前薪水详情以及部门编号dept_no
select
s.*,d.dept_no
from
salaries s,dept_manager d
where
d.to_date = '9999-01-01' and
s.to_date = '9999-01-01' and
d.emp_no = s.emp_no;
排名第一的通过代码:
select
salaries.emp_no,salaries.salary,salaries.from_date,salaries.to_date,dept_manager.dept_no
from
salaries
inner join
dept_manager
on
dept_manager.emp_no = salaries.emp_no and
dept_manager.to_date = '9999-01-01' and
salaries.to_date = '9999-01-01';
SQL INNER JOIN 关键字
在表中存在至少一个匹配时,INNER JOIN 关键字返回行。
个人理解,INNER JOIN 等同于 from a,b where a.name = b.name;
4.查找所有已经分配部门的员工的last_name和first_name
select
e.last_name,e.first_name,d.dept_no
from
employees e
inner join
dept_emp d
on
e.emp_no = d.emp_no;
5.查找所有员工的last_name和first_name以及对应部门编号dept_no
select
e.last_name,e.first_name,d.dept_no
from
employees e
left join
dept_emp d
on
e.emp_no = d.emp_no;
SQL LEFT JOIN 关键字
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
个人理解,多表联查时 用于返回所有信息(包括不满足 on 后限制条件的信息)
6.查找所有员工入职时候的薪水情况
select
e.emp_no,s.salary
from
employees e
inner join
salaries s
on
e.emp_no = s.emp_no and
e.hire_date = s.from_date
order by
e.emp_no desc;
7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
select
emp_no,count(emp_no) t
from
salaries
group by
emp_no
having
t > 15
having只用来在group by之后,having不可单独用,必须和group by用
having在聚合函数之后执行过滤条件
8.找出所有员工当前薪水salary情况
select distinct
salary
from
salaries
where
to_date = '9999-01-01'
order by
salary desc
因 distinct
效率不高,应用 group by
解决
select
salary
from
salaries
where
to_date = '9999-01-01'
group by
salary
order by
salary desc
9.获取所有部门当前manager的当前薪水情况
select
d.dept_no,d.emp_no,s.salary
from
dept_manager d
inner join
salaries s
on
d.emp_no = s.emp_no
where
s.to_date = '9999-01-01' and
d.to_date = '9999-01-01'
10.获取所有非manager的员工emp_no
select
e.emp_no
from
employees e
left join
dept_manager d
on
d.emp_no = e.emp_no
where
d.dept_no is null
11.获取所有员工当前的manager
select
de.emp_no, dm.emp_no as manager_no
from
dept_emp de
inner join
dept_manager dm
on
de.dept_no = dm.dept_no
where
dm.to_date = '9999-01-01' and
de.to_date = '9999-01-01' and
de.emp_no != dm.emp_no
SQL 中不等于可以用 != 也可以用 <>
12.获取所有部门中当前员工薪水最高的相关信息
select
d.dept_no, s.emp_no, max(s.salary) as salary
from
dept_emp d
inner join
salaries s
on
d.emp_no = s.emp_no
where
d.to_date = '9999-01-01' and
s.to_date = '9999-01-01'
group by
d.dept_no
group by
为 d.dept_no 分组,得到每组最高的薪水以及员工号
13.从titles表获取按照title进行分组
select
title, count(title) t
from
titles
group by
title
having
t >= 2
14.从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略。
select
title, count(distinct emp_no) t
from
titles
group by
title
having
t >= 2
15.查找employees表
select
*
from
employees
where
emp_no %2 =1 and
last_name != 'Mary'
order by
hire_date desc
16.统计出当前各个title类型对应的员工当前薪水对应的平均工资
select
t.title, avg(s.salary) avg
from
titles t
inner join
salaries s
on
t.emp_no = s.emp_no and
t.to_date = '9999-01-01' and
s.to_date = '9999-01-01'
group by
title
使用函数后,如 count
、 avg
、 max
等,都需要最后+ group by