标签:
SQL> --查询所有部门的信息和部门下的所有员工信息
SQL> select e.empno,e.ename,job,d.deptno,d.dname
2 from emp e,dept d
3 where e.deptno = d.deptno;
EMPNO ENAME JOB DEPTNO
---------- -------------------- ------------------ ----------
DNAME
----------------------------
7369 SMITH CLERK 20
RESEARCH
7499 ALLEN SALESMAN 30
SALES
7521 WARD SALESMAN 30
SALES
EMPNO ENAME JOB DEPTNO
---------- -------------------- ------------------ ----------
DNAME
----------------------------
7566 JONES MANAGER 20
RESEARCH
7654 MARTIN SALESMAN 30
SALES
7698 BLAKE MANAGER 30
SALES
EMPNO ENAME JOB DEPTNO
---------- -------------------- ------------------ ----------
DNAME
----------------------------
7782 CLARK MANAGER 10
ACCOUNTING
7788 SCOTT ANALYST 20
RESEARCH
7839 KING PRESIDENT 10
ACCOUNTING
EMPNO ENAME JOB DEPTNO
---------- -------------------- ------------------ ----------
DNAME
----------------------------
7844 TURNER SALESMAN 30
SALES
7876 ADAMS CLERK 20
RESEARCH
7900 JAMES CLERK 30
SALES
EMPNO ENAME JOB DEPTNO
---------- -------------------- ------------------ ----------
DNAME
----------------------------
7902 FORD ANALYST 20
RESEARCH
7934 MILLER CLERK 10
ACCOUNTING
已选择14行。
SQL> set linesize 200;
SQL> /
EMPNO ENAME JOB DEPTNO DNAME
---------- -------------------- ------------------ ---------- ----------------------------
7369 SMITH CLERK 20 RESEARCH
7499 ALLEN SALESMAN 30 SALES
7521 WARD SALESMAN 30 SALES
7566 JONES MANAGER 20 RESEARCH
7654 MARTIN SALESMAN 30 SALES
7698 BLAKE MANAGER 30 SALES
7782 CLARK MANAGER 10 ACCOUNTING
7788 SCOTT ANALYST 20 RESEARCH
7839 KING PRESIDENT 10 ACCOUNTING
7844 TURNER SALESMAN 30 SALES
7876 ADAMS CLERK 20 RESEARCH
EMPNO ENAME JOB DEPTNO DNAME
---------- -------------------- ------------------ ---------- ----------------------------
7900 JAMES CLERK 30 SALES
7902 FORD ANALYST 20 RESEARCH
7934 MILLER CLERK 10 ACCOUNTING
已选择14行。
SQL> -- 等值连接 用等号去连接2个表,但是一定要注意连接的2个表一定要有关系
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------------------------------------ -------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
SQL> selectn * from salgrade;
SP2-0734: 未知的命令开头 "selectn * ..." - 忽略了剩余的行。
SQL> select * from salgrade;
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
SQL> -- 查询员工信息和员工的工资级别
SQL> select e.empno,e.sal,s.grade,s.losal,s.hisal
2 from emp e,salgrade s
3 where e.sal between s.losal and s.hisal;
EMPNO SAL GRADE LOSAL HISAL
---------- ---------- ---------- ---------- ----------
7369 800 1 700 1200
7900 950 1 700 1200
7876 1100 1 700 1200
7521 1250 2 1201 1400
7654 1250 2 1201 1400
7934 1300 2 1201 1400
7844 1500 3 1401 2000
7499 1600 3 1401 2000
7782 2450 4 2001 3000
7698 2850 4 2001 3000
7566 2975 4 2001 3000
EMPNO SAL GRADE LOSAL HISAL
---------- ---------- ---------- ---------- ----------
7788 3000 4 2001 3000
7902 3000 4 2001 3000
7839 5000 5 3001 9999
已选择14行。
SQL> --不等值连接
SQL> --外连接
SQL> --统计每一个部门的人数
SQL> --显示部门号 部门名称 部门人数
SQL> select e.deptno,d.dname,count(*)
2 from emp e,dept d
3 where e.deptno = d.deptno
4 group by e.deptno,d.dname
5 ;
6 ;
7 ;‘;’‘’
8 ;
9 ;
;
*
第 5 行出现错误:
ORA-00911: 无效字符
SQL> select e.deptno,d.dname,count(*)
2 from emp e,dept d
3 where e.deptno = d.deptno
4 group by e.deptno,d.dname;
DEPTNO DNAME COUNT(*)
---------- ---------------------------- ----------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
SQL> select * from dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> /*
SQL> 外连接:分类左外连接和右外连接
SQL> 左外连接: where e.deptno = d.deptno 不成立的时候,无论右边是否有数据与之对应,左边的都要显示
SQL> where e.deptno = d.depton(+)
SQL> 右外连接: where e.deptno = d.deptno 不成立的时候,无论左边表是否有数据与之对应,右边表都会显示
SQL> where e.deptno(+) = d.deptno;
SQL> */
SQL> select d.deptno,d.dname,count(e.empno)
2 from emp e,dept d
3 where e.deptno(+) = d.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> select * from dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> -- 左外连接以左边表为主表,主表会显示所有,另一个表如果没有显示空,右外连接以右边表为主表.....
SQL> -- 内连接
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 19-4月 -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 23-5月 -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 e.ename 领导名字,e.empno 领导ID,p.ename 员工名字,p.empno 员工ID
2 from emp e,emp p
3 where e.empno = p.mgr;
领导名字 领导ID 员工名字 员工ID
-------------------- ---------- -------------------- ----------
FORD 7902 SMITH 7369
BLAKE 7698 ALLEN 7499
BLAKE 7698 WARD 7521
KING 7839 JONES 7566
BLAKE 7698 MARTIN 7654
KING 7839 BLAKE 7698
KING 7839 CLARK 7782
JONES 7566 SCOTT 7788
BLAKE 7698 TURNER 7844
SCOTT 7788 ADAMS 7876
BLAKE 7698 JAMES 7900
领导名字 领导ID 员工名字 员工ID
-------------------- ---------- -------------------- ----------
JONES 7566 FORD 7902
CLARK 7782 MILLER 7934
已选择13行。
SQL> select count(*) from emp e,emp p
2 where e.empno = p.mgr;
COUNT(*)
----------
13
SQL> select count(*) from emp e,emp p;
COUNT(*)
----------
196
SQL> -- 内连接只能做数据量小的表,对于大表层次查询
SQL> -- 层次查询
SQL> select e.ename 领导名字,e.empno 领导ID
2 from emp e
3 connect by prior empno = mgr
4 start with mgr is null;
领导名字 领导ID
-------------------- ----------
KING 7839
JONES 7566
SCOTT 7788
ADAMS 7876
FORD 7902
SMITH 7369
BLAKE 7698
ALLEN 7499
WARD 7521
MARTIN 7654
TURNER 7844
领导名字 领导ID
-------------------- ----------
JAMES 7900
CLARK 7782
MILLER 7934
已选择14行。
SQL> spool off;
标签:
原文地址:http://www.cnblogs.com/877599949yan/p/5286019.html