MySQL部门数据查询练习

  • 创建数据库和表
drop database if exists hrs;
create database hrs default charset utf8mb4;

use hrs;

create table tb_dept
(
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
);

insert into tb_dept values 
    (10, '会计部', '北京'),
    (20, '研发部', '成都'),
    (30, '销售部', '重庆'),
    (40, '运维部', '深圳');

create table tb_emp
(
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno),
foreign key (dno) references tb_dept (dno)
);
  • 添加数据
insert into tb_emp values 
    (7800, '张三丰', '总裁', null, 9000, 1200, 20),
    (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
    (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
    (3211, '张无忌', '程序员', 2056, 3200, null, 20),
    (3233, '丘处机', '程序员', 2056, 3400, null, 20),
    (3251, '张翠山', '程序员', 2056, 4000, null, 20),
    (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
    (5234, '郭靖', '出纳', 5566, 2000, null, 10),
    (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
    (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
    (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
    (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
    (3577, '杨过', '会计', 5566, 2200, null, 10),
    (3588, '朱九真', '会计', 5566, 2500, null, 10);
  • 查询月薪最高的员工姓名和月薪
select 
	ename as 员工姓名,
	sal as 月薪
from tb_emp where sal=(select max(sal) from tb_emp);
-- 或者
select ename, sal from tb_emp where sal>=all(select sal from tb_emp);
  • *查询员工的姓名和年薪((月薪+补贴)13)
select
	ename as 员工姓名,
	(ifnull(comm, 0) + sal)*13 as 年薪
from tb_emp
order by 年薪 desc;
  • 查询有员工的部门的编号和人数
select 
	dno as 部门编号,
	count(*) as 人数
from tb_emp
group by dno;
  • 查询所有部门名称和人数
select 
	dname as 部门名称,
    ifnull(total, 0) as 人数
from tb_dept left join(
	select dno, count(*) as total from tb_emp group by dno
) tb_temp on tb_dept.dno = tb_temp.dno;
  • 查询月薪最高的员工(Boss除外)的姓名和月薪
select 
	ename as 员工姓名,
	sal as 月薪
from tb_emp
order by sal desc limit 1,1;
-- 或者
select ename, sal from tb_emp 
where sal=(
	select max(sal) from tb_emp where mgr is not null
);
  • 查询月薪排第2名的员工的姓名和月薪
select ename, sal from tb_emp where sal=(
	select distinct sal from tb_emp order by sal desc limit 1,1
);

select ename, sal from tb_emp where sal=(
	select max(sal) from tb_emp where sal<(select max(sal) from tb_emp)
);
  • 查询月薪排第N名的员工的姓名和月薪
  • 查询月薪超过平均月薪的员工的姓名和月薪
select 
	ename as 员工姓名,
	sal as 月薪
from tb_emp
where sal > (
	select avg(sal) from tb_emp
);
  • 查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪
select  ename, t1.dno, sal from tb_emp t1 inner join(
	select dno, avg(sal) as avg_sal from tb_emp group by dno
) t2 on t1.dno=t2.dno and sal>avg_sal;
  • 查询部门中月薪最高的人姓名、月薪和所在部门名称
select ename, sal, dname
from tb_emp t1, tb_dept t2, (
	select dno, max
    (sal) as max_sal from tb_emp group by dno
) t3 where t1.dno=t2.dno and t1.dno=t3.dno and sal=max_sal;
  • 查询主管的姓名和职位
  • 提示,尽量少用in和not in运算,尽量少用distinct操作
  • 可以使用存在性判断(exists/ not exists)替代集合运算和去重操作
select ename, job from tb_emp where eno in(
	select distinct mgr from tb_emp where mgr is not null
);

select ename, job from tb_emp where eno=any(
	select distinct mgr from tb_emp where mgr is not null
);

-- 提示,尽量少用in 和 not in 运算,尽量少用distinct 操作
-- 可以使用存在性判断(exists / not exists)替代集合运算和去重操作
select ename, job from tb_emp t1 where exists(
	select 'x' from tb_emp t2 where t1.eno=t2.mgr
);

  • 查询月薪排名4~6名的员工的排名、姓名和月薪
select ename, sal from tb_emp order by sal desc limit 3,3;

select row_num, ename, sal from 
(select @a:=@a+1 as row_num, ename, sal 
from tb_emp, (select @a:=0) t1 order by sal desc) t2 
where row_num between 4 and 6;
  • 窗口函数
  • MySQL8有窗口函数:row_number() / rank() / dense_rank()
-- 窗口函数不适合业务数据库,只适合做离线数据分析
select ename, sal, 
	row_number() over (order by sal desc) as row_num,
	rank() over (order by sal desc) as ranking,
    dense_rank() over (order by sal desc) as dense_ranking
from tb_emp limit 3 offset 3;


select ename, sal, ranking from(
	select ename,  sal, dense_rank() over (order by sal desc) as ranking
from tb_emp
) tb_emp where ranking between 4 and 6;
  • 窗口函数主要用于解决TopN查询问题
  • 查询每个部门月薪排前两名的员工姓名和月薪
select ename, sal, dno from(
	select ename, sal, dno, rank() over (partition by dno order by sal desc) as ranking 
    from tb_emp
) tb_temp where ranking<=2;
  • MySQL5.7 不支持窗口函数的写法
select ename, sal, dno from tb_emp t1
where(
	select count(*) from tb_emp t2 
    where t1.dno=t2.dno and t2.sal > t1.sal
) < 2 order by dno asc, sal desc;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL多表查询是指在查询中同时涉及到多个表的操作。多表查询可以更加方便地获取到需要的数据,提供了更加灵活的查询方式。 在进行多表查询之前,我们首先需要对多个表之间的关系进行了解和分析。常见的多表查询方式包括联接查询、子查询以及多级嵌套查询等。 联接查询是最常见且常用的多表查询方式之一。通过联接查询,我们可以根据两个或多个表之间的关联字段,将它们连接在一起进行查询。 例如,我们有两个表A和B,它们通过一个共同的字段关联起来。我们可以使用联接查询语句如下: SELECT A.field1, B.field2 FROM A INNER JOIN B ON A.common_field = B.common_field; 在这个查询中,我们使用了INNER JOIN关键字将表A和表B连接起来,其中common_field是两个表之间的关联字段。通过这个联接查询,我们可以同时获取到表A和表B中的相关信息。 除了联接查询之外,我们还可以使用子查询和多级嵌套查询进行多表查询。 子查询是指在查询语句中嵌套了另一个查询语句。通过子查询,我们可以在一个查询中引用另一个查询的结果作为条件进行查询。 多级嵌套查询是指在一个查询语句中嵌套了多个子查询。通过多级嵌套查询,我们可以按照一定的顺序和逻辑进行复杂的查询操作。 综上所述,MySQL多表查询是一种可以同时涉及多个表进行查询的操作。通过联接查询、子查询和多级嵌套查询等方式,我们可以更加灵活地获取到需要的数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值