温故而知新。
emp 员工表(empno 员工号/ename 员工姓名/job 工作/mgr 上级编号/hiredate 受雇日期/sal 薪金
/comm 佣金/deptno 部门编号)
dept 部门表(deptno 部门编号/dname 部门名称/loc 地点)
工资= 薪金+ 佣金
1.列出至少有一个员工的所有部门。
2.列出薪金比“SMITH”多的所有员工。
3.列出所有员工的姓名及其直接上级的姓名。
4.列出受雇日期早于其直接上级的所有员工。
5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
6.列出所有“CLERK”(办事员)的姓名及其部门名称。
7.列出最低薪金大于1500 的各种工作。
8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
9.列出薪金高于公司平均薪金的所有员工。
10.列出与“SCOTT”从事相同工作的所有员工。
11.列出薪金等于部门30 中员工的薪金的所有员工的姓名和薪金。
12.列出薪金高于在部门30 工作的所有员工的薪金的员工姓名和薪金。
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
14.列出所有员工的姓名、部门名称和工资。
15.列出所有部门的详细信息和部门人数。
16.列出各种工作的最低工资。
17.列出各个部门的MANAGER(经理)的最低薪金。
18.列出所有员工的年工资,按年薪从低到高排序。
——–1———-
select dname from dept where deptno in(
select deptno from emp);
——–2———-
select * from emp where sal>(
select sal from emp where ename=’SMITH’);
——–3———-
select a.ename,(
select ename from emp b where b.empno=a.mgr) as bossname from emp a;
——–4———-
select a.ename from emp a where a.hiredate<(
select hiredate from emp b where b.empno=a.mgr);
——–5———-
select a.dname,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.comm,b.deptno
from dept a left join emp b on a.deptno=b.deptno;
——–6———-
select a.ename,b.dname from emp a join dept b
on a.deptno=b.deptno and a.job=’CLERK’;
——–7———-
select distinct job as HighSalJob from emp group by job having min(sal)>1500;
——–8———-
select ename from emp where deptno=(
select deptno from dept where dname=’SALES’);
——–9———-
select ename from emp where sal>(
select avg(sal) from emp);
——–10———
select ename from emp where job=(
select job from emp where ename=’SCOTT’);
——–11———
select a.ename,a.sal from emp a where a.sal in (
select b.sal from emp b where b.deptno=30) and a.deptno<>30;
——–12———
select ename,sal from emp where sal>(
select max(sal) from emp where deptno=30);
——–13———
select
(select b.dname from dept b where a.deptno=b.deptno) as deptname ,
count(deptno) as deptcount,
avg(sal) as deptavgsal
from emp a group by deptno;
——–14———
select
a.ename,
(select b.dname from dept b where b.deptno=a.deptno) as deptname,
sal
from emp a;
——–15———
select
a.deptno,
a.dname,
a.loc,
(select count(deptno) from emp b where b.deptno=a.deptno group by b.deptno) as deptcount
from dept a;
——–16———
select job,avg(sal) from emp group by job;
——–17———
select deptno,min(sal) from emp where job=’MANAGER’ group by deptno;
——–18———
select ename,(sal+nvl(comm,0))*12 as salpersal from emp order by salpersal;
ORACLE 子句查询,分组等
A.同表子查询作为条件
a. 给出人口多于Russia(俄国)的国家名称SELECT name FROM bbc
WHERE population>
(SELECT population FROM bbc
WHERE name=’Russia’)
b.给出’India'(印度), ‘Iran'(伊朗)所在地区的所有国家的所有信息SELECT * FROM bbc
WHERE region IN
(SELECT region FROM bbc
WHERE name IN (‘India’,’Iran’))
c.给出人均GDP 超过’United Kingdom'(英国)的欧洲国家. SELECT name FROM bbc
WHERE region=’Europe’ AND gdp/population >
(SELECT gdp/population FROM bbc
WHERE name=’United Kingdom’)
d.这个查询实际上等同于以下这个:
select e1.ename from emp e1,(select empno from emp where ename = ‘KING’) e2 whe
re e1.mgr = e2.empno;
你可以用EXISTS 写同样的查询,你只要把外部查询一栏移到一个像下面这样的子查询环境
中就可以了:
select ename from emp e
where exists (select 0 from emp where e.mgr = empno and ename = ‘KING’);
当你在一个WHERE 子句中写EXISTS 时,又等于向最优化传达了这样一条信息,即你想让外
部查询先运行,使用每一个值来从内部查询(假定:EXISTS=由外而内)中得到一个值。
B.异表子查询作为条件
a.select * from studentExam where studentid=( select studentid from student whe
re name=’吴丽丽’);
b.select * from studentexam where studentid in (
select studentid from student) order by studentid;
c.select * from student where studentid in (select studentid from studentexam w
here mark>80);
3.select studentexam.mark,studentexam.studentid as seid, student.studentid,stud
ent.name from studentexam,student where student.studentid=studentexam.studentid;
过滤分组:
顺序为先分组,再过滤,最后进行统计(实际值).
select studentid,count(*) as highpasses from studentexamwhere mark>70group by s
tudentid;
假使我们不想通过数据表中的实际值,而是通过聚合函数的结果来过过滤查询的结果.
select studentid,avg(mark) as averagemarkfrom studentexamwhere avg(mark)<50 or
avg(mark)>70group by studentid;(此句错误,where 句子是不能用聚合函数作条件的)此
时要用having.
select studentid,avg(mark) from studentexam group by studentid having avg(mark)
>70 or avg(mark)<50;
select studentid,avg(mark) from studentexam where studentid in(1,7,9,5)group by
studentid having avg(mark)>70;(先分组,再过滤,再having 聚合,最后再统计).
select studentid ,avg(mark) as averagemarkfrom studentexamwhere examid in(5,8,1
1)group by studentidhaving avg(mark)<50 or avg(mark)>70;
返回限定行数查询:
select name from student where rownum<=10;
oracle 中使用rownum 关键字指定,但该关键字必须在where 子句中与一个比较运算符一起
指定,而不能与order by 一起配合便用,因为rownum 维护的是原始行号.如果需要用group
by\order by 就用子句查询作表使用的方法:
select studentid,averagemark from(select studentid,avg(mark) as averagemarkfrom
studentexamgroup by studentid order by averagemark desc)where rownum<=10;
版权声明:本文为husheng8891原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。