`
supportopensource
  • 浏览: 521989 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Oracle SQL的练习(转)

 
阅读更多
--列出员工表中每个部门的员工数,和部门 no
select t.deptno,count(1) from scott.emp t group by t.deptno;

--列出员工表中每个部门的员工数(员工数必须大于 3),和部门名称
select t.deptno,t1.dname,count(1)
from scott.emp t
left join scott.dept t1 on t.deptno=t1.deptno
group by t.deptno,t1.dname
having count(1)>3;

--找出工资比 ward 多的员工
select a.* from scott.emp a, scott.emp b 
where a.sal>b.sal and b.ename='ward'; 

--列出所有员工的姓名和其上级的姓名
select a.empno,a.ename,a.mgr,b.empno,b.ename from scott.emp a, scott.emp b
where a.mgr=b.empno;

--以职位分组,找出平均工资最高的两种职位
select * from (select job,avg(sal) from scott.emp group by job order by avg(sal) desc )where rownum<3;

--查找出不在部门 20,且比部门 20 中任何一个人工资都高的员工姓名、部门名称
--比任何人的工资高就是比工资最高的还要高
select * from scott.emp where sal>(select max(sal) from scott.emp where deptno='20') and deptno<>'20';

--得到平均工资大于 2000 的工作职种
select job from scott.emp group by job having avg(sal)>2000;

--分部门得到  工资大于 2000 的所有员工的平均工资,并且平均工资还要大于 2500
select t.deptno,avg(sal) from scott.emp t where t.sal>2000 group by t.deptno having avg(sal)>2500;

--得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置
select * from scott.dept
where deptno = (
  select c.deptno from (select deptno,sum(sal) from scott.emp group by deptno order by sum(sal)) c
  where rownum=1
);

--分部门得到平均工资等级为 3 级(等级表)的部门编号
select b.dno from scott.salgrade a,
(select t.deptno as dno,avg(t.sal) as avgsal
from scott.emp t
group by t.deptno) b
where a.grade=3 and b.avgsal between a.losal and a.hisal;

--查找出部门 10 和部门 20 中,工资最高第 3 名到工资第 5 名的员工的员工名字,部门名字,部门位置
select * from (select rownum no,b.* from (select a.* from scott.emp a where a.deptno in(10,20) order by a.sal desc) b) c
where c.no>=3 and c.no<=5;
select c.ename,d.dname,d.loc from (select rownum no,b.* from (select a.* from scott.emp a where a.deptno in(10,20) order by a.sal desc) b) c,scott.dept d
where c.deptno=d.deptno and c.no>=3 and c.no<=5;

--查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入
select a.empno,a.ename,a.sal+nvl(a.comm,0) from scott.emp a,scott.emp b 
where a.mgr=b.empno and (a.sal+nvl(a.comm,0))>(b.sal+nvl(b.comm,0));

--查找出工资等级不为 4 级的员工的员工名字,部门名字,部门位置
select c.ename,c.deptno,d.loc,c.sal from scott.dept d,
(select a.ename,a.deptno,a.sal from scott.emp a,scott.salgrade b
where b.grade<>4 and a.sal between b.losal and b.hisal) c
where d.deptno=c.deptno;

--查找出职位和'MARTIN'  或者'SMITH'一样的员工的平均工资
select avg(sal) from scott.emp where job in (select job from scott.emp where ename in('MARTIN','SMITH')) group by job;

--查找出不属于任何部门的员工
select * from scott.emp where deptno is null or deptno not in(select deptno from scott.dept);

--按部门统计员工数,查处员工数最多的部门的第二名到第五名(列出部门名字,部门位置)
select c.dname,c.loc from (select rownum no,a.* from(select t.deptno,count(1) from scott.emp t group by t.deptno order by count(1) desc) a)b ,scott.dept c
where b.deptno=c.deptno and b.no>=2 and b.no<=5;

--查询出 king 所在部门的部门号\部门名称\部门人数
select b.deptno,a.dname,b.count from scott.dept a,
(select deptno,count(1) as count from scott.emp 
  where deptno in(select deptno from scott.emp where ename = 'KING') group by deptno) b
where a.deptno=b.deptno;

select b.deptno,a.dname,b.count from scott.dept a,
(select deptno,t.count from(select deptno,count(1) as count from scott.emp  group by deptno) t
 where t.deptno in(select deptno from scott.emp where ename = 'KING')) b
where a.deptno=b.deptno;

select a.deptno 部门号,a.dname 部门名称,(select count(*) from scott.emp where deptno in (select deptno from scott.emp where ename ='KING')) 部门人数  
from scott.dept a,scott.emp b where a.deptno=b.deptno and b.ename='KING'; 

--查询出 king 所在部门的工作年限最大的员工名字
select t.ename from(select rownum no,a.* from(select ename,hiredate from scott.emp where deptno in(select deptno from scott.emp where ename='KING') order by hiredate) a) t
where t.no=1;

--查询出工资成本最高的部门的部门号和部门名称
select deptno,dname from scott.dept where deptno in(
select b.deptno from (select rownum no,a.* from (select deptno,sum(sal) from scott.emp group by deptno order by sum(sal) desc) a) b where b.no=1 );






分享到:
评论

相关推荐

    Oracle SQL:经典练习题(附答案)

    本文提供的经典练习题旨在帮助初学者熟悉SQL语句的编写,特别是针对Oracle数据库特有的功能。以下将详细讲解涉及的知识点。 1. **基本查询操作**:练习题中包含了查询员工表(emp)和部门表(dept)的基础信息,...

    Oracle数据库SQL练习

    本资料包针对Oracle数据库的SQL使用提供了丰富的练习资源,旨在帮助用户深入理解和熟练掌握SQL语言在Oracle环境中的应用。 "SQL10G.CHM"可能是一个关于Oracle 10g版本的SQL参考手册或教程,其中包含了该版本SQL语法...

    oracle sql练习题

    oracle数据库练习题,对新手有帮助,看看没有害处。

    Oracle SQL练习题及答案_最全最新

    本资料包“Oracle SQL练习题及答案_最全最新”提供了一系列关于Oracle SQL的基础查询语句实例,旨在帮助学习者巩固和提升SQL语法的应用能力。 首先,Oracle SQL的基本查询语句包括SELECT、FROM、WHERE、GROUP BY、...

    oracle数据库sql练习题(含答案)

    oracle数据库sql练习题(含答案)用于练习增删改查操作。

    Oracle的SQL练习

    本人工作中使用oracle用到的常用SQL. 包括常用函数、块、游标、数据包、正则等初级/中级/高级知识.

    Oracle高级sql学习与练习

    Oracle高级SQL学习与练习涵盖了数据库编程中的一系列高级主题,旨在帮助数据库开发者和管理员提高解决复杂问题的能力。在Oracle数据库系统中,高级SQL技能是进行高效数据管理、查询优化和复杂数据处理的基础。 1. ...

    oracle经典sql语句练习题和答案

    本资源“oracle经典sql语句练习题和答案”提供了在scott用户下的两个典型表格——emp(员工表)和dept(部门表)的实践操作题目,旨在帮助用户提升SQL技能。 首先,让我们来了解这两个核心表格。`emp`表通常包含...

    oracle sql练习全集(附答案)

    这份“Oracle SQL练习全集”提供了丰富的练习题和答案,旨在帮助学习者深入理解和掌握SQL语言的各种特性和功能。以下是对Oracle SQL核心知识点的详细解释: 1. **SQL基础** - **SELECT语句**:SQL中最常用的是...

    一套Oracle SQL练习题及答案,有建表语句

    一套Oracle SQL练习题及答案,有建表语句 基本上每个sql语法都用到了,sql不好的可以看看,也有业务场景

    Oracle SQL PL/SQL 练习资料

    Oracle SQL和PL/SQL是...通过这些练习,你可以逐步掌握Oracle SQL和PL/SQL的精髓,为实际的数据库开发工作打下坚实的基础。无论你是初学者还是有一定经验的开发者,这份资料都能帮助你巩固技能,提升解决问题的能力。

    Oracle SQL(SQL for Oracle)

    Oracle SQL,全称为结构化查询语言在Oracle数据库环境中的应用...书籍可能会包含丰富的示例和练习,以加深对Oracle SQL的理解和实践能力。对于任何在Oracle环境中工作的数据库专业人员,这都是一本非常有价值的参考书。

    oracle sql 大全(三个文档)

    Oracle SQL大全,正如标题所言,是一套涵盖了Oracle数据库系统中SQL语言的综合学习资源。这套资料主要由三个文档组成,分别是"Oracle8i_9i数据库基础.pdf"、"oracle1.pdf"和"SQL基础.pdf",它们分别从不同角度深入浅...

    Oracle SQL/Plus练习题

    通过不断地实践和解决这些练习题,你可以逐步提升你的Oracle SQL/Plus技能,这对于任何涉及Oracle数据库管理的工作都是非常有价值的。记住,熟练掌握SQL语言不仅限于理论知识,更重要的是通过实际操作来加深理解和...

    Oracle SQL 官方教程

    Oracle SQL 官方教程是学习和理解Oracle数据库系统中SQL语言的重要资源,编号为007的教程可能是一个系列教程的一部分。在这个教程中,你将深入掌握如何在Oracle环境中使用SQL进行数据查询、操作、分析以及数据库管理...

    Oracle存储过程LP/SQL练习题(含答案)

    Oracle存储过程LP/SQL练习题(含答案) 几个练习题

    MLDN魔乐科技_Oracle课堂15_SQL语法练习2

    《Oracle课堂15:SQL语法练习2》是MLDN魔乐科技推出的一堂深入学习SQL语言的课程,主要针对已经掌握基本SQL概念的学习者,旨在通过一系列实践练习,提升他们在Oracle数据库环境中运用SQL的能力。本课程的核心内容...

Global site tag (gtag.js) - Google Analytics