`
yidongkaifa
  • 浏览: 4066058 次
文章分类
社区版块
存档分类
最新评论

oracle部门员工表关系查询

 
阅读更多
查询各个部门内员工个数
department:deptno dname
employee: deptno empno personnel
select e.deptno,count(e.empno) from (select * from emp em group by em.empno)e ————这是自己想出来的,是错误的
select e.deptno,count(e.empno) from (select * from emp em group by em.deptno)e ————这是自己想出来的,是错误的
select e.deptno,count(e.empno) from emp e group by e.deptno ————这是正确的写法,要按部门分组

select d.dname,count(e.empno) from emp e,dept d where e.deptno = d.deptno group by d.dname ————这是正确的写法,要按部门分组



网上资料1

1.列出至少有一个员工的所有部门。
select deptno,dname,loc from dept
where deptnoin (select deptno fromemp);
2.列出薪金比“SMITH”多的所有员工。
select * from emp
where sal>(select sal from emp
where ename=SMITH);
3.列出所有员工的姓名及其直接上级的姓名。
select a.ename,b.ename mgname from emp a,emp b
where a.mgr=b.empno;
4.列出受雇日期早于其直接上级的所有员工。
select * from emp a,emp b
where a.mgr=b.empno and a.hiredate

  select a.hiredate,b.hiredate from emp a,emp b
where a.mgr=b.empno and a.hiredate5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select dname,emp.* from emp,dept
where emp.deptno(+)=dept.deptno;
6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select ename,dname from emp,dept
where emp.deptno=dept.deptno and emp.job=CLERK;
7.列出最低薪金大于1500的各种工作。
select job from emp
groupby job
having min(sal)>1500;
8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select ename from (select deptno from dept where dname=SALES) a,emp
where a.deptno = emp.deptno;
9.列出薪金高于公司平均薪金的所有员工。
select emp.* from emp
where sal>(select avg(sal) from emp);
10.列出与“SCOTT”从事相同工作的所有员工。
select emp.* from emp
where job in (select job from emp where ename=SCOTT);
11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
select ename,sal from emp
where sal in (select sal from emp where deptno=30);
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select ename,sal from emp
where sal > (select max(sal) from emp where deptno=30);
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
14.列出所有员工的姓名、部门名称和工资。
select ename,dname,(nvl(comm,0)+sal) from emp,dept
where emp.deptno = dept.deptno;
15.列出从事同一种工作但属于不同部门的员工的一种组合。
select a.* from emp a,emp b
where a.job=b.job and a.deptno<>b.deptno;
16.列出所有部门的详细信息和部门人数。
select dept.*,(select count(*) from emp where dept.deptno = emp.deptno) as pop from dept;
17.列出各种工作的最低工资。
select job,min((nvl(comm,0)+sal)) from emp
group by job;
18.列出各个部门的MANAGER(经理)的最低薪金。
select min(sal) from emp
where job=MANAGER
group by deptno;
19.列出所有员工的年工资,按年薪从低到高排序
select ename,(nvl(comm,0)+sal)*12 a from emp
order by a asc;



网上资料2

1. select * from emp;

2. select empno, ename, job from emp;

3. select empno 编号, ename 姓名, job 工作 from emp;

4. select job from emp;

5. select distinct job from emp;

6. select distinct empno, job from emp;
说明:因为雇员编号不重复, 所以此时证明所有的列没有重复,所以不能消除掉重复的列.

7. 查询出雇员的编号, 姓名, 工作, 但是显示的格式:编号是: 7369 的雇员, 姓名是: smith, 工作是: clear
select '编号是: ' || empno || '的雇员, 姓名是: ' || ename || ', 工作是: ' || job from emp;

8. 求出每个雇员的姓名及年薪
select ename, sal * 12 income from emp;

9. 求出工资大于 1500 的所有雇员信息
select * from emp where sal > 1500;

10. 查询每月可以得到奖金的雇员信息
select * from emp where comm is not null;

11. 查询没有奖金的雇员信息
select * from emp where comm is null;

12. 查询出基本工资大于 1500 同时可以领取奖金的雇员信息
select * from emp where sal > 1500 and comm is not null;

13. 查询出基本工资大于 1500 或者可以领取奖金的雇员信息
select * from emp where sal > 1500 or comm is not null;

14. 查询出基本工资不大于 1500 或者不可以领取奖金的雇员信息
select * from emp where not(sal > 1500 and comm is not null);

15. 查询基本工资大于 1500, 但是小于 3000 的全部雇员信息
select * from emp where sal > 1500 and sal < 3000;

16. 查询基本工资大于等于 1500, 但是小于等于 3000 的全部雇员信息
select * from emp where sal >= 1500 and sal <= 3000;
select * from emp where sal between 1500 and 3000;

17. 查询出在 1981 年雇佣的全部雇员信息(1981 年 1 月 1 日 到 1981 年 12 月 31 日之间的雇佣的雇员)
select * from emp where hiredate between '1-1月-81' and '31-12月-81';

18. 要求查询出姓名是 smith 的雇员信息
select * from emp where ename = 'SMITH';

19. 要求查询出雇员是 7369, 7499, 7521 的雇员的具体信息
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;
select * from emp where empno in(7369, 7499, 7521);

20. 要求查询出雇员不是 7369, 7499, 7521 的雇员的具体信息
select * from emp where empno not in(7369, 7499, 7521);

21. 要求查询出姓名是 smith, allen, king 的雇员信息
select * from emp where ename in('SMITH', 'ALLEN', 'KING');

22. 查询出所有雇员姓名中第二个字母包含 "M" 的雇员信息
select * from emp where ename like '_M%';

23. 查询出雇员姓名中包含字母 M 的雇员信息
select * from emp where ename like '%M%';

24. 要求查询出在 1981 年雇佣的雇员信息
select * from emp where hiredate like '%81%';

25. 查询工资中包含 5 的雇员信息
select * from emp where sal like '%5%';

26. 查询雇员编号不是 7369 的雇员信息
select * from emp where empno != 7369;
select * from emp where empno <> 7369;

27. 要求按照工资由低到高排序
select * frm emp order by sal;
select * from emp order by sal asc;

28. 要求按照工资由高到低排序
select * from emp order by sal desc;

29. 要求查询出 20 部门的所有雇员信息,查询的信息按照工资由高到低排序,如果工资相等,则按照雇佣日期由早到晚排序.
select * from emp where deptno = 20 order by sal desc, hiredate asc;

30. 将小写字母变为大写字母
select upper('hello') from dual;

31. 将大写字母变为小写字母
select lower('HELLO WORLD') from dual;

32. 要求查询出姓名是 smith 的雇员信息
select * from emp where ename = upper('smith');

33. 使用 initcap() 函数将单词的第一个字母大写
select initcap('hello world') from dual;

34. 将雇员表中的雇员姓名变为开头字母大写
select initcap(ename) from emp;

35. 将字符串 "hello" 和 "world" 进行串联
select concat('hello ', 'world') from dual;

36. 对字符串进行操作的常用字符处理函数
select substr('hello', 1, 3) 截取字符串, length('hello') 字符串的长度, replace('hello', 'l', 'x') 字符串替换 from dual;
select substr('hello', 0, 3) 截取字符串, length('hello') 字符串的长度, replace('hello', 'l', 'x') 字符串替换 from dual;

37. 显示所有雇员的姓名及姓名的后三个字符
select ename, substr(ename, length(ename) -2) from emp;
select ename, substr(ename, -3, 3) from emp;

38. 使用数值函数执行四舍五入操作
select round(789.536) from dual;

39. 要求将 789.536 数值保留两位小数
select round(789.536, 2) from dual;

40. 要求将 789.536 数值中的整数的十位进行四舍五入进位
select round(789.536, -2) from dual;

41. 采用 trunc() 函数不会保留任何小数,而且小数点也不会执行四舍五入的操作
select trunc(789.536) from dual;

42. 通过 trunc() 也可以指定小数点的保留位数
select trunc(789.536, 2) from dual;

43. 作用负数表示位数
select trunc(789.536, -2) from dual;

44. 使用 mod() 函数可以进行取余的操作
select mod(10, 3) from dual;

45. 显示 10 部门雇员进入公司的星期数(当前日期 - 雇佣日期 = 天数 / 7 = 星期数)
select empno, ename, round((sysdate - hiredate) / 7) from emp where deptno = 10;

46. 日期函数
months_between(): 求出给定日期范围的月数
add_months(): 在指定的日期上加上指定的月数, 求出之后的日期
next_day(): 指定日期的下一个日期
last_day(): 求出给定日期当月的最后一天日期

47.
select empno, ename, months_between(sysdate, hiredate) from emp;
select empno, ename, round(months_between(sysdate, hiredate)) from emp;

48. select sysdate, add_months(sysdate, 4) from dual;

49. select next_day(sysdate, '星期一') from dual;

50. select last_day(sysdate) from dual;

51. 转换函数
to_char(): 转换成字符串
to_number(): 转换成数字
to_date(): 转换成日期

52. 查询所有雇员的雇员编号, 姓名, 雇佣日期
select empno,
ename,
to_char(hiredate, 'yyyy') year,
to_char(hiredate, 'mm') months,
to_char(hiredate, 'dd') day
from emp;

select empno, ename, to_char(hiredate, 'yyyy-mm-dd') from emp;

select empno, ename, to_char(hiredate, 'fmyyyy-mm-dd') from emp;

53. 查询所有雇员的编号, 姓名和工资
select empno, ename, sal from emp;
select empno, ename, to_char(sal, '99,999') from emp;
select empno, ename, to_char(sal, 'L99,999') from emp;
select empno, ename, to_char(sal, '$99,999') from emp;

54. select to_number('123') + to_number('123') from dual;

55. 将一个字符串转换成日期类型
select to_date('2009-01-01', 'yyyy-mm-dd') from dual;

56. 求出每个雇员的年薪(要求加上奖金)
select empno, ename, sal, comm, (sal + comm) * 12 from emp;
select empno, ename, sal, comm, nvl(comm, 0), (sal + nvl(comm, 0)) * 12 income from emp;

57. decode() 函数类似于 if....elsif...else 语句
select decode(1, 1, '内容是 1', 2, '内容是 2', 3, '内容是 3') from dual;

58. 查询出雇员的编号, 姓名, 雇佣日期及工作, 要求将雇员的工作替换成以下信息:
select empno 雇员编号,
ename 雇员姓名,
hiredate 雇佣日期,
decode(job,
'CLERK', '业务员',
'SALESMAN', '销售人员',
'MANAGER', '经理',
'ANALYST', '分析员',
'PRESIDENT', '总裁'
) 职位
from emp;

59. 笛卡尔积(交差连接)
select * from emp, dept;
select * from emp cross join dept;

60. 内连接
select * from emp e, dept d where e.deptno = d.deptno;
select * from emp e inner join dept d on e.deptno = d.deptno;
select * from emp e join dept d on e.deptno = d.deptno;


61. 自然连接
select * from emp natural join dept;
select * from emp e join dept d using(deptno);

62. 要求查询出雇员的编号, 姓名, 部门的编号, 名称, 地址
select e.empno, e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno;

63. 要求查询出雇员的姓名, 工作, 雇员的直接上级领导姓名
select e.ename, e.job, m.ename from emp e, emp m where e.mgr = m.empno;

64. 要求查询出雇员的姓名, 工作, 雇员的直接上级领导姓名以及部门名称
select e.ename, e.job, m.ename, d.dname from emp e, emp m, dept d where e.mgr = m.empno and e.deptno = d.deptno;

65. 要求查询出每个雇员的姓名, 工资, 部门名称, 工资在公司的等级(salgrade), 及其领导的姓名及工资所在公司的等级
select e.ename, e.sal, d.dname, s.grade, m.ename, m.sal, ms.grade
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno
and e.sal between s.losal and s.hisal
and e.mgr = m.empno
and m.sal between ms.losal and ms.hisal;

select e.ename,
e.sal,
d.dname,
decode(s.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级'),
m.ename,
m.sal,
decode(ms.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级')
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno
and m.sal between ms.losal and ms.hisal;

66. select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e inner join dept d on e.deptno = d.deptno;

67. 左外连接
select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno(+);
select empno, ename, d.deptno, dname, loc from emp e left outer join dept d on e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e left join dept d on e.deptno = d.deptno(+);

68. 右外连接
select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno(+) = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e right outer join dept d on e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e right join dept d on e.deptno = d.deptno;

69. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno;

70. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno(+);

71.
select * from emp e, dept d where e.deptno = d.deptno and d.deptno = 30;
select * from emp e inner join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e natural join dept d where deptno = 30;
select * from emp e join dept d using(deptno) where deptno = 30;

72.
select e.ename, d.deptno, d.dname, d.loc from emp e right outer join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e right join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno(+) = d.deptno;

73. select count(ename) from emp;

74. select min(sal) from emp;

75. select max(sal) from emp;

76. select sum(sal) from emp;

77. select avg(sal) from emp;

78. select sum(sal) from emp where deptno = 20;

79. select avg(sal) from emp where deptno = 20;

80. 求出每个部门的雇员数量
select deptno, count(deptno) from emp group by deptno;
select deptno, count(empno) from emp group by deptno;

81. 求出每个部门的平均工资
select deptno, avg(sal) from emp group by deptno;

82. 按部门分组, 并显示部门的名称, 及每个部门的员工数
select d.dname, count(e.empno) from emp e, dept d
where e.deptno = d.deptno
group by d.dname;

select d.deptno, d.dname, temp.c
from (select deptno, count(e.empno) c from emp e group by e.deptno) temp, dept d
where temp.deptno = d.deptno;

83. 要求显示出平均工资大于 2000 的部门编号和平均工资
select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;

84. 显示非销售人员工作名称以及从事同一工作雇员的月工资的总和,并且要满足从事同一工作的雇员的月工资合计大于 5000, 输出结果按月工资的合计升序排序.
select job, sum(sal) su from emp where job <> 'SALESMAN' group by job having sum(sal) > 5000 order by su;

select temp.job, sum(temp.sal) s
from (select job, sal from emp e where job <> 'SALESMAN') temp
group by temp.job
having sum(temp.sal) > 5000
order by s;

85. 求出平均工资最高的部门工资
select max(avg(sal)) from emp group by deptno;

86. 要求查询出比雇员编号为 7654 工资高的所有雇员信息
select * from emp where sal >(select sal from emp where empno = 7654);

87. 要求查询出工资比 7654 高, 同时与 7788 从事相同工作的全部雇员信息
select * from emp
where sal >(select sal from emp where empno = 7654)
and job = (select job from emp where empno = 7788);

88. 要求查询出工资最低的雇员姓名, 工作, 工资
select ename, job, sal from emp where sal = (select min(sal) from emp);

89. 要求查询出: 部门名称,部门的员工数,部门的平均工资,部门的最低收入雇员的姓名
select d.dname, temp.c, temp.a, e.ename
from dept d,
(select deptno, count(empno) c, avg(sal) a, min(sal) m from emp group by deptno) temp,
emp e
where d.deptno = temp.deptno and e.sal = temp.m;

select d.deptno, temp.dname, temp.c, temp.a, e.ename, e.sal
from
(select d.dname , count(e.empno) c, avg(e.sal) a, min(e.sal) m
from emp e, dept d
where e.deptno = d.deptno
group by d.dname) temp,
emp e,
dept d
where temp.m = e.sal
and temp.dname = d.dname;

90. 求出每个部门的最低工资的雇员的信息
select * from emp where sal in(select min(sal) from emp group by deptno);
select * from emp where sal =any(select min(sal) from emp group by deptno);
select * from
(select min(sal) m from emp group by deptno) temp,
emp e
where e.sal = temp.m;

91. 范例 90 中, 比子查询条件中最低(小)的工资要大的雇员信息
select * from emp where sal >any(select min(sal) from emp group by deptno);
select * from emp where sal > (select min(min(sal)) from emp group by deptno);

92. 范例 90 中, 比子查询条件中最高(大)的工资要小的雇员信息
select * from emp where sal <any(select min(sal) from emp group by deptno);
select * from emp where sal < (select max(min(sal)) from emp group by deptno);

93. 范例 90 中, 比子查询条件中最高(大)的工资要大的雇员信息
select * from emp where sal >all(select min(sal) from emp group by deptno);
select * from emp where sal > (select max(min(sal)) from emp group by deptno);

94. 范例 90 中, 比子查询条件中最低(小)的工资要小的雇员信息
select * from emp where sal <all(select min(sal) from emp group by deptno);
select * from emp where sal < (select min(min(sal)) from emp group by deptno);

95. 查找出 20 部门中没有奖金的雇员信息
select * from emp where (sal, nvl(comm, -1)) in (select sal, nvl(comm, -1) from emp where deptno = 20);
select * from emp where deptno = 20 and comm is null;

96. union 操作符返回两个查询选定的所有不重复的行
select deptno from emp union select deptno from dept;

97. union all 操作符合并两个查询选定的所有行,包括重复的行
select deptno from emp union all select deptno from dept;

98. intersect 操作符只返回两个查询都有的行
select deptno from emp intersect select deptno from dept;

99. minus 操作符只返回由第一个查询选定但是没有被第二个查询选定的行, 也就是在第一个查询结果中排除在第二个查询结果中出现的行
select deptno from dept minus select deptno from emp;


分享到:
评论

相关推荐

    Oracle多表连接练习

    1. 列出所有员工的姓名、部门名称和工资 Select e.ename,d.dname,e.sal From emp e,dept d Where e.deptno=d.deptno; 2. 列出所有部门的详细信息和部门人数 Select d.deptno,d.dname, d.loc,count(e.empno) From ...

    EBS HR模块表结构

    EBS HR模块表结构 HR 数据字典 人员基本信息表 职位表 组织表 等等

    oracle实验报告

    3、 通过dept表查询出所有部门号,对每个部门雇员的工资进行调整,将工资高于(包含$2000)$2000的雇员每人增加$500,将工资低于$2000的雇员每人增加到$2000。但应注意雇员工资调整后不应大于$10000,否则显示出错信息,...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    SQL(Structured Query Language)结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。同时也是数据库脚本文件的扩展名。  SQL语言主要包含5个部分  数据定义...

    Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐)--随书源代码

    他认为对于SQL的学习是永无止境的,相信每一个查询Oracle数据库的人都需要精通SQL语言,才能写出高效的查询。他参与本书的编写就是为了帮助别人实现这一目标。 目录 封面 -11 封底 -10 扉页 -9 版权 -8 版权声明 -7...

    oracle数据库经典题目

    19.视图是一个表示表的数据的数据库对象,它允许用户从一个表或一组表中通过一定的查询语句建立一个“虚表”。 20.序列是一种可被多个用户使用的用于产生一系列唯一数字的数据库对象。尤其适合多用户环境中,可以...

    基于ssh开发的企业人事管理系统

    企业人事管理系统分六大功能模块,其具体模块功能描述如下。 1.系统管理 ...6.1.3 部门调动员工报表:统计部门员工调动情况的报表功能。 6.1.4 岗位调动员工报表:统计岗位调动员工的情况报表功能。

    Oracle客户数据平台白皮书

    “Oracle 客户数据平台...有了一个管理主要客户信息的平台,现在以及将来的每个部门职能就都能够针对相同的记录来获取事务处理活动了,从而改善报表的精确性、员工的生产率、客户分析的可靠性以及日常的客户关系处理。

    java_jsp项目源码_移动ssh项目(struts+spring+hibernate+oracle)130222.rar

    2. 部门管理:提供部门的增删改查功能,支持部门间的关联和层级管理,方便企业内部的组织架构调整。 3. 员工管理:实现员工信息的录入、修改、查询和删除,支持员工的职位调整和岗位变更,满足企业人事管理的需求。 ...

    基于web的工资管理系统课程设计.doc

    系统概述 1.1开发背景 工资管理是一个单位不可缺少的部分,因为工资是一个单位与职员之间最重要的关系纽 带,它是职员辛勤劳动的主要成果,不同的职员工资不同,所产生的价值也不同。一直 以来人们都是使用传统的...

    第二热电厂MIS系统数据库设计论文

    3.2.1.2 部门表 (sys_Dept) 31 3.2.1.3 角色表 (sys_role) 31 3.2.1.4 角色权限表 (sys_RolePriv) 31 3.2.1.5 模块菜单表 (Sys_MMenu) 31 3.2.1.6 权限表 (sys_priv) 32 3.2.1.7 人员职务关系表 (sys_UserDuty) 32 ...

    系统分析师-数据库系统2-真题-无答案.docx

    数据统计 某公司的部门(部门号,部门名,负责人,电话)、商品(商品号,商品名称,单价,库存量)和职工(职工号,姓名,住址)三个实体之间的关系如表4-8、表4-9和表4-10所示。假设每个部门有一位负责人和一部电话,但...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例030 为新员工分配部门 40 实例031 用Switch语句根据消费金额计算折扣 41 实例032 判断用户输入月份的季节 42 2.4 循环控制 43 实例033 使用while与自增运算符循环遍历 数组 43 实例034 使用for循环输出杨辉三角 ...

Global site tag (gtag.js) - Google Analytics