Oracle next day notes .sql

select * from bonus;

select * from salgrade;

/ *
Multi-table query:
Cartesian product: the product is actually two tables, but does not mean much in the actual development

 格式: select * from 表1,表2   

*/
select * from emp;
select * from dept;

select * from emp, dept;

E1 * from EMP SELECT, WHERE e1.deptno Dept D1 = d1.deptno;
/ *
the coupling:
Implicit inner coupling:
the equivalent coupling: where e1.deptno = d1.deptno;
Unequal inner coupling: where e1. deptno <> d1.deptno;
self-coupling: connecting their own
show the coupling:
SELECT * from table 1 inner join table 2 on the connection conditions

       inner 关键字可以省略

*/
select * from emp e1, dept d1 where e1.deptno <> d1.deptno;

- Query employee number, employee name, number, manager's name, the manager of
the SELECT e1.empno, e1.ename, e1.mgr, m1.ename
from emp E1, the WHERE e1.mgr emp M1 = m1.empno;

- Query employee number, employee name, department name of the employee, the manager of the numbers, manager's name,
the SELECT e1.empno, e1.ename, d1.dname, e1.mgr, m1.ename
from emp E1, emp M1, the dept d1 the WHERE e1.mgr = m1.empno and e1.deptno = d1.deptno;

- Query employee number, employee name department name, department name of the employee, the manager of the numbers, manager's name, the manager of
the SELECT e1.empno, e1.ename, d1.dname, e1.mgr, m1.ename, d2.dname
from E1 EMP, EMP M1, Dept D1, D2 Dept
WHERE
e1.mgr = m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
;

- Query employee number, employee name, department name of the employee, the employee wage scale, the number of managers, manager's name, the name of the department managers
select e1.empno, e1.ename, d1.dname, s1.grade , e1.mgr , m1.ename, d2.dname
from EMP E1, M1 EMP, Dept D1, D2 Dept, salgrade S1
WHERE
e1.mgr = m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and E1. the BETWEEN s1.losal and s1.hisal SAL
;

- Query employee number, employee name, department name of the employee, the employee wage scale, the number of managers, manager's name, department name of the manager, the manager of the wage scale
select e1.empno, e1.ename, d1.dname, s1 . Grade, e1.mgr, m1.ename, d2.dname, s2.grade
from EMP E1, M1 EMP, Dept D1, D2 Dept, salgrade S1, S2 salgrade
WHERE
e1.mgr = m1.empno
and e1.deptno = D1. DEPTNO
m1.deptno = d2.deptno and
and e1.sal BETWEEN s1.losal and s1.hisal
and m1.sal BETWEEN s2.losal and s2.hisal
;

- Query employee number, employee name, department name of the employee, the employee wage scale, the number of managers, the manager's name, department name of the manager, the manager of the wage scale
- would pay grade 1,2,3,4 show into Chinese one two three ...

e1.empno SELECT,
e1.ename,
d1.dname,
Case s1.grade
When the then. 1 'a'
When the then 2 'two'
When the then. 3 'three'
When the then. 4 'four'
the else
'five'
end "level",
e1.mgr,
m1.ename,
d2.dname,
decode (s2.grade,. 1, 'a', 2, 'two', 3, 'three', 4, 'four' , 'five') "level"
from EMP E1, M1 EMP, Dept D1, D2 Dept, salgrade S1, S2 salgrade
WHERE
e1.mgr = m1.empno
and e1.deptno = d1.deptno
and m1.deptno = D2. DEPTNO
and e1.sal BETWEEN s1.losal and s1.hisal
and m1.sal BETWEEN s2.losal and s2.hisal
;

- Query employee name and location in which the employee's department
select e1.ename, d1.loc from emp e1, dept d1 where e1.deptno = d1.deptno;

select * from emp e1 inner join dept d1 on e1.deptno = d1.deptno;

/ *
External connection: (standard, general writing)
left outer: left outer all records join the left table, if the right table there is no corresponding record, it displays an empty
right outer connection: right outer all records join the right in the table, if table left no corresponding record is displayed empty
outer keyword can be omitted

Oracle中的外连接: (+) 实际上是如果没有对应的记录就加上空值
      select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);            

*/
select * from emp e1 left outer join dept d1 on e1.deptno = d1.deptno;
insert into emp(empno,ename) values(9527,‘HUAAN’);
select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);

select * from emp e1 right outer join dept d1 on e1.deptno = d1.deptno;
select * from emp e1,dept d1 where e1.deptno(+) = d1.deptno;

/ *
Subquery: query nested query; used to solve complex query
query highest wage employee information
single-row subquery:>> = = <<= <> =!

     多行子查询: in not in  >any >all exists not exists
           
           查询领导信息

* /
- highest employee salary information inquiry
-1 query highest pay --5,000
SELECT max (SAL) from EMP;
-2 wage equals the maximum wage.
SELECT * from EMP WHERE SAL = (SELECT max (SAL) from EMP) ;

- check out the employee's wages higher than 7654, while in 7788 the same job and employee information
-1 wages of employees 7654 1250.
The SELECT SAL from emp the WHERE empno = 7654;
-2.7788 work performed ANALYST
the SELECT emp the Job from the WHERE empno = 7788 ;
. conditions -3 merge two
select * from emp where sal> 1250 and job = 'ANALYST';

select * from emp where sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788);

- Query the minimum wage employee information for each department and his department of information
-1 queries minimum wage for each department, group statistics.
The SELECT deptno, min (SAL) minsal Group by deptno from emp;
-2 wages equal to his. sector minimum wage at
the SELECT *
from emp E1,
(the SELECT deptno, min (SAL) minsal Group by deptno from emp) T1
the WHERE e1.deptno = t1.deptno and e1.sal = t1.minsal;
. -3 sector related queries information
SELECT *
from EMP E1,
(SELECT DEPTNO, min (SAL) from EMP Group minsal by DEPTNO) T1,
Dept D1
WHERE e1.deptno t1.deptno and e1.sal = = = d1.deptno t1.minsal and e1.deptno ;

/ *
Inner join, single-row subquery, multiple-row subqueries

  in 
  not in
  any 
  all
  exists 
  
  通常情况下, 数据库中不要出现null  最好的做法加上Not null
  null值并不代表不占空间, char(100) null 100个字符

* /
- Query information leading
-1 to query all the manager's number
the SELECT MGR from emp;
the SELECT DISTINCT MGR from emp;
-2 result.
The SELECT * from emp in the WHERE empno (MGR from the SELECT emp);

- Query information is not led by
select * from emp where empno not in ( the SELECT MGR from emp);
the SELECT * from emp the WHERE empno <> All (the SELECT MGR from emp);
- the correct wording
select * from emp where empno not in ( select mgr from emp where mgr is not null);

- check out any high employee payroll department employee information than No. 10 20 30 10
the SELECT * from emp the WHERE SAL> the any (SAL from the SELECT emp the WHERE deptno = 10);

- check out all the employee's salary is higher than 20 department employees information 102 030
No. -1.20 highest salary 5000
the SELECT max (SAL) from emp the WHERE deptno = 20;
. -2 employee information
select * from emp where sal> ( select max (sal) from emp where deptno = 20);

----- multi-line sub-query is finished above this question
--------- No. 20 departments of all staff salaries (800 2975 ...)
the SELECT SAL from emp the WHERE deptno = 20;
------- - greater than the set of all
select * from emp where sal> all (select sal from emp where deptno = 20);

/ *
EXISTS (query): the meaning of existence, a table to determine which records whether there is a table with the other
as a Boolean value to handle:
When there query results, is to return to true
false otherwise returns
the amount of data a relatively large time is very efficient
* /
SELECT * from EMP WHERE EXISTS (SELECT * from EMP WHERE DEPTNO = 1234567);
SELECT * from EMP WHERE. 3. 4 =;

select * from emp where exists(select * from emp where deptno = 20);

- Query information department employees
select * from dept d1 where exists ( select * from emp e1 where e1.deptno = d1.deptno);

- find the employee table of the top three highest salary (in descending order)
the SELECT * from emp the Order by SAL desc;
/ *
rownum: pseudo-column, the system automatically generates the one used to indicate the line number

   rownum是Oracle中特有的用来表示行号的, 默认值/起始值是 1 ,在每查询出结果之后,再添加1
   
   rownum最好不能做大于号判断,可以做小于号判断
   
   SQL执行顺序
   from .. where ..group by..having .. select..rownum..order by

/
Select rownum,e1.
from emp e1;

- Query all records rownum is greater than 2,
the SELECT rownum, E1 * from emp E1 the WHERE rownum> two; -. There is no record

- rownum query for all records greater than or equal to 1.
select rownum, e1 * from emp e1 where rownum> = 1;.

- query rownum <6 all records
select rownum, e1 * from emp e1 where rownum <6;.

–rownum 排序
Select rownum,e1.* from emp e1 order by sal;

- find the employee table of the top three highest salary
the SELECT * from emp E1 E1 the Order by SAL desc;.
- The above results are treated as a table, then the query
select rownum, t1 * from (select e1 * from.. emp e1 order by sal desc) t1 ;

- as long as the three records show the first
select rownum, t1 * from (select e1 * from emp e1 order by sal desc.) T1 where rownum <4;.

- find the employee table salary greater than the average salary of employees in this sector
-1 packet statistics department of the average salary.
The SELECT deptno, AVG (SAL) avgsal Group by deptno from emp;
-2 wages> average wage in this sector.
The SELECT * from emp E1 , (SELECT DEPTNO, AVG (SAL) from EMP Group avgsal by DEPTNO) T1
WHERE e1.deptno = t1.deptno and e1.sal> t1.avgsal;
/ *
correlated subquery, non-correlated subquery
* /
SELECT * from EMP e where sal> (select avg ( sal) from emp e2 group by deptno having e.deptno = e2.deptno);

/ *
Count the number of employees each year recruits
* /
the SELECT the HireDate from emp;
- display only in
the SELECT the TO_CHAR (the HireDate, 'yyyy') from emp;
- group statistics
select to_char (hiredate, 'yyyy' ) yy, count (1) cc from emp group by to_char (hiredate , 'yyyy');

select yy
from
(select to_char(hiredate,‘yyyy’) yy,count(1) cc from emp group by to_char(hiredate,‘yyyy’)) tt;

select case yy when ‘1987’ then cc end
from
(select to_char(hiredate,‘yyyy’) yy,count(1) cc from emp group by to_char(hiredate,‘yyyy’)) tt;

select case yy when ‘1987’ then cc end “1987”
from
(select to_char(hiredate,‘yyyy’) yy,count(1) cc from emp group by to_char(hiredate,‘yyyy’)) tt;

- removing rows null values
SELECT SUM (Case YY When '1987' the then CC End) "1987"
from
(SELECT TO_CHAR (HireDate, 'YYYY') YY, COUNT (. 1) CC from EMP Group by (HireDate TO_CHAR, 'yyyy')) tt;

- statistics of the total number of employees
the SELECT SUM (CC) "TOTAL"
from
(the SELECT the TO_CHAR (the HireDate, 'yyyy') YY, COUNT (1) CC from emp Group by (the HireDate the TO_CHAR, 'yyyy')) tt;

–将1987 和TOTAL 合并在一起
select
sum(cc) “TOTAL”,
sum(case yy when ‘1987’ then cc end) “1987”
from
(select to_char(hiredate,‘yyyy’) yy,count(1) cc from emp group by to_char(hiredate,‘yyyy’)) tt;

–显示所有年份的结果
select
sum(cc) “TOTAL”,
sum(case yy when ‘1980’ then cc end) “1980”,
sum(case yy when ‘1981’ then cc end) “1981”,
sum(case yy when ‘1982’ then cc end) “1982”,
sum(case yy when ‘1987’ then cc end) “1987”
from
(select to_char(hiredate,‘yyyy’) yy,count(1) cc from emp group by to_char(hiredate,‘yyyy’)) tt;

/ *
ROWID: recording dummy columns per row address stored in the real physical
rownum: line number, after the recording of each query, a line number will be added
/
SELECT ROWID, E.
From EMP E;

- removing duplicates in Table
Create Table P (
name VARCHAR2 (10)
);

insert into p values ( 'Huangwei Fu');
insert into p values ( 'Betty');
insert into p values ( 'Yang');

delete from p where

select rowid,p.* from p;
select distinct * from p;

delete from p p1 where rowid > (select min(rowid) from p p2 where p1.name = p2.name);

/ *
Rownum: paging query
using only the oracle subquery to do paging query
/
- Query 6 - 10 record
the SELECT rownum, emp.
From emp;

select rownum hanghao, emp.* from emp;

select * from (select rownum hanghao, emp.* from emp) tt where tt.hanghao between 6 and 10;

/ *
Set operations:
union: the query results are merged two
intersection
difference set

   所有的查询结果可能不是来自同一张表,  
     emp  2000年
          2017年 手机 详细信息 emp2017

* /
- wages and salaries greater than 1500, or under 20 departments
select * from emp where sal> 1500 or deptno = 20;

- salary greater than 1500
the SELECT * from emp the WHERE SAL> 1500;
staff at -20 No. departments
select * from emp where deptno = 20 ;

- and set operations: All Union Union
/ *
Union: de-duplicated, and the ordering
union all: not removing duplicate
* /
SELECT * from EMP WHERE SAL> 1500
Union
SELECT * from EMP WHERE DEPTNO = 20 is;

select * from emp where sal > 1500
union all
select * from emp where deptno = 20;

/ *
Intersection operator: intersect

* /
- salary greater than 1500, and at 20 department employees
the SELECT * from emp the WHERE SAL> 1500;
the SELECT * from emp the WHERE deptno = 20;

select * from emp where sal > 1500
intersect
select * from emp where deptno = 20;

/ *
Set difference operation: two subtraction result
* /
--1981 recruits annual income (not including the CEO and managers)
--1981 annual income recruits
select * from emp where to_char (hiredate , 'yyyy') = '1981';

- President and managers
select * from emp where job = ' PRESIDENT' or job = 'MANAGER';

select * from emp where to_char(hiredate,‘yyyy’)=‘1981’
minus
select * from emp where job = ‘PRESIDENT’ or job = ‘MANAGER’;

/ *
Note the set operation:
type 1 of the column to be consistent
2. Sequential Write
3. Number of columns to be consistent, if inadequate, with nulls filled
* /
SELECT ename, SAL from EMP WHERE SAL> 1500
Union
SELECT ename, SAL 20 is from EMP WHERE DEPTNO =;
- type columns do not match
SELECT ename, SAL from EMP WHERE SAL> 1500
Union
SELECT SAL, ename from EMP WHERE DEPTNO = 20 is;

- the number of columns do not match
SELECT ename, SAL, DEPTNO from EMP WHERE SAL> 1500
Union
SELECT ename, SAL from EMP WHERE DEPTNO = 20 is;

select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,null from emp where deptno = 20;

select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,66 from emp where deptno = 20;

select * from emp;
select * from dept;

Published 22 original articles · won praise 0 · Views 195

Guess you like

Origin blog.csdn.net/qq_41518179/article/details/104675229