资讯详情

03dml查询

– 如果存在,删除名称hrs的数据库

drop database if exists `hrs`; 

– 创建名为hrs指定默认字符集的数据库

create database `hrs` default charset utf8mb4; 

– 切换到hrs数据库

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`) ); 

– 插入4个部门

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 not null comment 所在部门编号, primary key (`eno`), constraint `fk_emp_mgr` foreign key (`mgr`) references tb_emp (`eno`), constraint `fk_emp_dno` foreign key (`dno`) references tb_dept (`dno`) ); 

– 插入14个员工

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 t1.ename as 姓名,    t2.ename as 主管姓名  from tb_emp as t1    left join tb_emp as t2     on t1.mgr = t2.eno;              select t1.ename,t2.ename  from tb_emp as t1 left join tb_emp as t2  on t1.mgr = t2.eno; 

– 查询月薪最高的员工姓名和月薪

-- 方法一: select ename as 姓名,sal  from tb_emp     order by sal desc     limit 1; -- 方法二: select ename,sal from tb_emp where sal = (select max(sal)     from tb_emp); -- 方法三: -- all / any  SELECT ename, sal FROM tb_emp WHERE sal >= ALL (SELECT sal      FROM tb_emp);  select ename,sal from tb_emp as t1 where (select count(*) from tb_emp as t2   where t1.sal<t2.sal)=0; -- 方法四:(计数法:没有人工资比我高,也就是工资最高。可推广到工资前三名或后三名) select ename,sal from tb_emp as t1 where (  select count(*)     from tb_emp as t2     where t2.sal > t1.sal ) = 0; -- eg:月薪 top 3 select ename,sal from tb_emp as t1 where (  select count(*)     from tb_emp as t2     where t2.sal > t1.sal ) < 3; -- 月薪 最低的3人 select ename,sal from tb_emp as t1 where (  select count(*)     from tb_emp as t2     where t2.sal < t1.sal ) < 3;  -- 方法五:存在性判断 exists / not exists  select ename,sal  from tb_emp as t1  where not exists(select sal /*这里什么都可以查,比如1,x , ename 都可*/      from tb_emp as t2                     where t2.sal > t1.sal); -- eg:存在性判断例题 select now() from dual/*此处的from dual 可以省略*/ where exists (select 'x',100 from tb_emp     where dno=20);                  select ename,sal from tb_emp as t1 where exists(select 'x' from tb_emp as t2     where t1.sal < t2.sal); 

– 查询月薪Top员工姓名及月薪

select ename,sal from tb_emp order by sal desc limit 3; select ename,sal from tb_emp as t1 where (select count(*)   from tb_emp as t2         where t2.sal>t1.sal)<3; 

– 查询员工姓名和年薪(年薪)=(sal comm)*13)

SELECT ename AS 姓名, (sal   (COALESCE(comm, 0))) * 13 AS 年薪 FROM tb_emp ORDER BY 年薪 DESC; 

– 查询部门的编号和人数

SELECT dno, COUNT(*) FROM tb_emp GROUP BY dno; 

– 查询部门人数超过5人的部门编号和人数

select dno,count(*) as 人数 from tb_emp group by dno having 人数 > 5; 

– 查询各部门的名称和人数

select dname,coalesce(人数,0) from tb_dept left join(select dno,count(*) as 人数 from tb_emp      group by dno) as tmp     on tb_dept.dno = tmp.dno; -- 方法一: selet dname as 部门名称,count(eno) as 部门人数 
from tb_dept as t1
	left join tb_emp as t2
		on t1.dno = t2.dno
group by dname;
-- 方法二:
select dname as 部门名称,
	coalesce(total,0) as 部门人数
from tb_dept as t1
	left join (select dno,count(*) as total
				from tb_emp
                group by dno) as t2
			on t1.dno = t2.dno;

– 查询月薪超过平均月薪的员工的姓名和月薪

select ename,sal from tb_emp
where tb_emp.sal > (select round(avg(sal),2) from tb_emp);

– 查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪

SELECT ename,sal,dno 
FROM tb_emp as t1 natural join (select dno,avg(sal) as avg_sal 
								from tb_emp
								group by dno) as t2
where sal > avg_sal;

– 查询部门中月薪最高的人姓名、月薪和所在部门名称

SELECT ename, sal,dname 
FROM tb_dept
 NATURAL JOIN (SELECT ename, sal,t1.dno 
 FROM tb_emp
  INNER JOIN ( SELECT dno, max( sal ) AS max_sal FROM tb_emp GROUP BY dno ) AS t1 ON tb_emp.dno = t1.dno 
  AND tb_emp.sal = t1.max_sal 
 ) AS t2;
 
 
 select ename, sal, dname
 from tb_emp as t1
			natural join tb_dept as t2
			natural join (select dno,max(sal) as max_sal
							from tb_emp
							group by dno) as t3
 where sal = max_sal;

delete ename,sal,dname
from tb_dept as t1
	natural join tb_emp as t2
where (dno,sal) in (select dno,max(sal)
					from tb_emp
                    group by dno);

– 查询主管的姓名和职位

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 as t1
where exists (select 'X' from tb_emp as t2
				where t1.eno = t2.mgr);

– 查询普通员工(不是主管)的姓名和职位

SELECT ename, job
FROM tb_emp
WHERE eno not IN (SELECT DISTINCT mgr
        FROM tb_emp
        WHERE mgr IS NOT NULL);
        
select ename, job from tb_emp as t1
where not exists (select 'X' from tb_emp as t2
				where t1.eno = t2.mgr);

– 查询月薪排名4~6名的员工排名、姓名和月薪

select @a := 0;/*定义变量*/
-- select @a := @a + 2;

select * from (select ename, sal, @a := @a + 1 as 排名
				from tb_emp,(select @a := 0) as tmp
				order by sal desc) as tmp
where 排名 between 4 and 6;

– 窗口函数,分析函数(性能很差,适合数据分析,不适开发),sql 8

select * from(
select ename,sal,row_number() over (order by sal desc) as 排名1,
				rank() over (order by sal desc) as 排名2,
				dense_rank() over (order by sal desc) as 排名3
				from tb_emp)as tmp

where 排名3 between 4 and 6;

– 查询每个部门月薪排前2名的员工姓名、月薪和部门编号

select ename,sal,t1.dno from tb_emp as t1
where (select count(*)
		from tb_emp as t2
        where t2.dno = t1.dno 
        and t1.sal > t2.sal) < 2
order by dno asc,sal desc;

select ename,sal,dno from(
select ename,sal,dno,
		rank() over (partition by dno order by sal desc) as rn
		from tb_emp) as tmp
where rn <= 2;

标签: le36sn08dno传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台