例题

练习#

1、查询所有员工的年工资、所在部门的名称,按年薪从低往高排序

分析:

​ 1.要查询哪些字段、表

​ 2.连接条件

1查询所有员工的年工资、所在部门的名称,按年薪从低往高排序。

select e.sal*12,nvl(comm,0)年工资,dname 部门名称 
from emp e,dept d
where e.deptno=d.deptno 
order by sal asc

2查询所有员工的编号、姓名,及其上级领导的编号、姓名。显示结果按领导的年工资降序

1.要查询哪些字段、表

	e.empno  ,e.ename , b.empno   ,b.ename

	emp e,emp b 

2.连接条件
	e.mgr = b.empno 

select 	e.empno 员工编号  ,e.ename 员工名字, b.empno 领导表的员工编号  ,b.ename 领导表的员工名字
from emp e,emp b 
where e.mgr = b.empno(+)
order by b.sal*12+nvl(b.comm,0) desc ;


3查询非销售人员的 :工作名称,以及从事同一工作员工的月工资之和,要求月工资之和大于5000,输出结果按月工资之和降序排列。

1.要查询哪些字段、表
select job ,sum(sal)
from emp
where job <> 'SALESMAN'  -- 不是销售的工作有哪些
	group by job
	having sum(sal)>5000
	order by  sum(sal) desc ;


salesmane: 10  ->8000
dev:  8  -->7000
test :6  -->4000

5查询所有领取奖金和不领取奖金的员工人数、平均工资。

-- select count(*),avg(sal)
-- from emp 
-- where comm is not null and comm > 0
-- union
-- select count(*),avg(sal)
-- from emp
-- where comm is null or comm =0

select deptno,job,sum(sal) from emp group by deptno,job;
-- 各个部门的总工资
select deptno,sum(sal) from emp group by deptno
-- 总工资
select sum(sal) from emp


null:常量
select deptno,job,sum(sal) from emp group by deptno,job
union
select deptno,null,sum(sal) from emp group by deptno
union
select null,null,sum(sal) from emp

增强的group byrollup()

select deptno,job,sum(sal) from emp group by rollup (deptno,job)

6查询每种工作的最低工资,以及领取该工资的员工姓名。


7查询出工资不超过2500的人数最多的部门名称

SQL> select d.deptno,d.dname from dept d,emp e
  2  where d.deptno = e.deptno
  3  and e.sal <= 2500
  4  group by d.deptno , d.dname
  5  having count(*) =
  6  (select max(count(*)) from emp where sal <= 2500 group by deptno);

    DEPTNO DNAME
---------- --------------
        30 SALES

8查询出管理 员工人数最多的人的名字 和他管理的人的名字。

管理 员工人数最多的人的 编号

z: 30
ls(7369): 40
w: 20

查询:最多是多少

  1. 先根据mgr分组: 根据领导分组
    最多是多少
select max(cn) from (
select  count(*) cn from emp group by mgr
);

select mgr from emp group by mgr  having  count(*) =  (select max(cn) from (
select  count(*) cn from emp group by mgr
));

8查询出管理 员工人数最多的人的名字 和他管理的人的名字。

管理 员工人数最多的人的 领导编号 -》名字 和他管理的人的名字。

select  b.ename , e.ename  from emp e
inner join  emp b  
on  e.mgr = b.empno
where e.mgr=(select mgr from emp group by mgr  having  count(MGR) =  (select max(cn) from (
select  count(MGR) cn from emp group by mgr
))) ;



select *from emp e,emp b
where e.mgr = b.empno ;

UNION(并集):返回各个查询的所有记录,不包括重复记录。
UNION ALL(并集):返回各个查询的所有记录,包括重复记录。
INTERSECT(交集):返回两个查询共有的记录。
MINUS(差集):返回 包含在第一个查询中,但不包含在第二个查询中的记录。

3):解析#

误区:阅读理解

1)、正确做法:翻译 文字->SQL

select job , sum(sal)

where job <>  'salseman'

​	job,sum(sal)

group by job  

having sum(sal) > 5000   

order by sum(sal) desc;

group by job     意思(看你这个工作是不是销售,或者是销售对比下去)

salesmane :10 --> 8000  (比如有10个salsemane,工资总和:8000元)
  select job ,sum(sal) 
  from emp 
  where job<> 'SALESMAN' 
  group by job 
  having sum(sal)>5000 
  order by sum(sal) desc;

统计各个年份的入职人数,以及总入职人数#

select * from emp
select count(*) 总人数,
sum(decode (to_char(hiredate,'yyyy'),1980,1,0))"1989",
sum(decode (to_char(hiredate,'yyyy'),1981,1,0))"1981",
sum(decode (to_char(hiredate,'yyyy'),0087,1,0))"1987"
from emp;

posted @   Rzk  阅读(199)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
主题色彩
点击右上角即可分享
微信分享提示