SQL> -- 等值连接 采用=号
SQL> --查询员工信息,要求:显示员工的姓名,工资,部门号,部门名称
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> break on null
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select ename,sal,deptno,dname
2 from emp,dept
3 where emp.deptno=dept.deptno;
select ename,sal,deptno,dname
*
第 1 行出现错误:
ORA-00918: 未明确定义列
SQL> select e.ename,e.sal,d.deptno,d.dname
2 from emp e,dept d
3 where e.deptno=d.deptno;
ENAME SAL DEPTNO DNAME
---------- ----- ---------- --------------
SMITH 800 20 RESEARCH
ALLEN 1600 30 SALES
WARD 1250 30 SALES
JONES 2975 20 RESEARCH
MARTIN 1250 30 SALES
BLAKE 2850 30 SALES
CLARK 2450 10 ACCOUNTING
SCOTT 3000 20 RESEARCH
KING 5000 10 ACCOUNTING
TURNER 1500 30 SALES
ADAMS 1100 20 RESEARCH
ENAME SAL DEPTNO DNAME
---------- ----- ---------- --------------
JAMES 950 30 SALES
FORD 3000 20 RESEARCH
MILLER 1300 10 ACCOUNTING
已选择14行。
SQL> --多表查询时,采用表的别名,来对同样的列做区分
SQL> host cls
SQL> -- 不等值连接
SQL> --查询员工的工资水平
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
SQL> select * from salgrade;
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
SQL> select e.ename,e.sal,s.grade
2 from emp e,salgrade s
3 where e.sal>=s.losal and e.sal<=s.hisal;
ENAME SAL GRADE
---------- ----- ----------
SMITH 800 1
JAMES 950 1
ADAMS 1100 1
WARD 1250 2
MARTIN 1250 2
MILLER 1300 2
TURNER 1500 3
ALLEN 1600 3
CLARK 2450 4
BLAKE 2850 4
JONES 2975 4
ENAME SAL GRADE
---------- ----- ----------
SCOTT 3000 4
FORD 3000 4
KING 5000 5
已选择14行。
SQL> select e.ename,e.sal,s.grade
2 from emp e,salgrade s
3 where e.sal between s.losal and s.hisal;
ENAME SAL GRADE
---------- ----- ----------
SMITH 800 1
JAMES 950 1
ADAMS 1100 1
WARD 1250 2
MARTIN 1250 2
MILLER 1300 2
TURNER 1500 3
ALLEN 1600 3
CLARK 2450 4
BLAKE 2850 4
JONES 2975 4
ENAME SAL GRADE
---------- ----- ----------
SCOTT 3000 4
FORD 3000 4
KING 5000 5
已选择14行。
SQL> host cls
SQL> --按部门统计员工的人数,要求显示:部门号,部门的名称和部门人数
SQL> select d.deptno,d.dname,count(e.empno)
2 from dept d,emp e
3 where d.deptno=e.detpno
4 group by d.deptno,d.dname;
where d.deptno=e.detpno
*
第 3 行出现错误:
ORA-00904: "E"."DETPNO": 标识符无效
SQL> select d.deptno,d.dname,count(e.empno)
2 from dept d,emp e
3 where d.deptno=e.detpno
4 group by dept.deptno,dept.dname;
group by dept.deptno,dept.dname
*
第 4 行出现错误:
ORA-00904: "DEPT"."DNAME": 标识符无效
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------------------------------- -------- --------------------------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> select d.deptno,d.dname,count(e.empno)
2 from dept d,emp e
3 where d.deptno=e.deptno
4 group by dept.deptno,dept.dname;
group by dept.deptno,dept.dname
*
第 4 行出现错误:
ORA-00904: "DEPT"."DNAME": 标识符无效
SQL> select d.deptno,d.dname,count(e.empno)
2 from dept d,emp e
3 where d.deptno=e.deptno
4 group by d.deptno,d.dname;
DEPTNO DNAME COUNT(E.EMPNO)
---------- -------------- --------------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
SQL> select count(*) from emp where deptno=10;
COUNT(*)
----------
3
SQL> select count(*) from emp where deptno=20;
COUNT(*)
----------
5
SQL> select count(*) from emp where deptno=30;
COUNT(*)
----------
6
SQL> select count(*) from emp where deptno=40;
COUNT(*)
----------
0
SQL> --外连接:当条件不满足时,任然希望某些列显示
SQL> --分为:左外连接、右外连接
SQL> select d.deptno,d.dname,count(e.empno)
2 from dept d,emp e
3 where d.deptno = e.deptno(+)
4 group by d.deptno,d.dname;
DEPTNO DNAME COUNT(E.EMPNO)
---------- -------------- --------------
10 ACCOUNTING 3
40 OPERATIONS 0
20 RESEARCH 5
30 SALES 6
SQL> --左外连接:等号左边所代表的集合,无论条件是否成立,均在结果集中显示
SQL> -- 写法:where d.deptno = e.deptno(+)
SQL> --右外连接: 等号右边所代表的集合,无论条件是否成立,均在结果集中显示
SQL> -- 写法:where d.deptno(+) = e.deptno
SQL> host cls
SQL> --自连接:
SQL> --查询员工和老板上下级关系
SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------- -------- --------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> select e.ename ||'老板是:' || b.ename
2 from emp e,emp b
3 where e.mgr=b.empno;
E.ENAME||'老板是:'||B.ENAME
----------------------------
SMITH老板是:FORD
ALLEN老板是:BLAKE
WARD老板是:BLAKE
JONES老板是:KING
MARTIN老板是:BLAKE
BLAKE老板是:KING
CLARK老板是:KING
SCOTT老板是:JONES
TURNER老板是:BLAKE
ADAMS老板是:SCOTT
JAMES老板是:BLAKE
E.ENAME||'老板是:'||B.ENAME
----------------------------
FORD老板是:JONES
MILLER老板是:CLARK
已选择13行。
SQL> --这种用表的别名方式将同一张表视为两张的自连接操作好么?
SQL> --自连接一般情况下,只适用于小表
SQL> --为了解决这样的问题,Oracle提出了层次查询
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择14行。
SQL> select level,empno,ename
2 from emp
3 connect by prior empno = mgr
4 start with empno=7839;
LEVEL EMPNO ENAME
---------- ---------- ----------
1 7839 KING
2 7566 JONES
3 7788 SCOTT
4 7876 ADAMS
3 7902 FORD
4 7369 SMITH
2 7698 BLAKE
3 7499 ALLEN
3 7521 WARD
3 7654 MARTIN
3 7844 TURNER
LEVEL EMPNO ENAME
---------- ---------- ----------
3 7900 JAMES
2 7782 CLARK
3 7934 MILLER
已选择14行。
SQL> ed
已写入 file afiedt.buf
1 select level,empno,ename
2 from emp
3 connect by prior empno = mgr
4 start with empno=7839
5* order by 1
SQL> /
LEVEL EMPNO ENAME
---------- ---------- ----------
1 7839 KING
2 7566 JONES
2 7698 BLAKE
2 7782 CLARK
3 7902 FORD
3 7521 WARD
3 7900 JAMES
3 7934 MILLER
3 7499 ALLEN
3 7788 SCOTT
3 7654 MARTIN
LEVEL EMPNO ENAME
---------- ---------- ----------
3 7844 TURNER
4 7876 ADAMS
4 7369 SMITH
已选择14行。
SQL> --层次查询性能高于自连接
SQL> --打开sql执行时间的开关
SQL> set timing on
SQL> /
LEVEL EMPNO ENAME
---------- ---------- ----------
1 7839 KING
2 7566 JONES
2 7698 BLAKE
2 7782 CLARK
3 7902 FORD
3 7521 WARD
3 7900 JAMES
3 7934 MILLER
3 7499 ALLEN
3 7788 SCOTT
3 7654 MARTIN
LEVEL EMPNO ENAME
---------- ---------- ----------
3 7844 TURNER
4 7876 ADAMS
4 7369 SMITH
已选择14行。
已用时间: 00: 00: 00.14
SQL> select e.ename ||'老板是:' || b.ename
2 from emp e,emp b
3 where e.mgr=b.empno;
E.ENAME||'老板是:'||B.ENAME
----------------------------
SMITH老板是:FORD
ALLEN老板是:BLAKE
WARD老板是:BLAKE
JONES老板是:KING
MARTIN老板是:BLAKE
BLAKE老板是:KING
CLARK老板是:KING
SCOTT老板是:JONES
TURNER老板是:BLAKE
ADAMS老板是:SCOTT
JAMES老板是:BLAKE
E.ENAME||'老板是:'||B.ENAME
----------------------------
FORD老板是:JONES
MILLER老板是:CLARK
已选择13行。
已用时间: 00: 00: 00.37
SQL> set timing off
SQL> spool off