Oracle Database - a joint multi-table queries & subquery

Multi-table joint inquiry

When you need to get data distributed across multiple, considering the use of the joint inquiry
1, SQL92 mode
2, SQL99 way

SQL92 way

1, Cartesian product: a plurality of data tables is one to one, the results obtained for the Cartesian product of multi-table.
  Number of results for the product of the number of all tables.
  * from EMP SELECT, Dept 
2, equivalent connection filtering
  concept: first table Cartesian product, and then screening with screening to equivalent conditions.
  Note: Conditions to be screened for the value field of the same name field may be different
  query employee name, job, salary, department name
  select * from emp, dept where emp.deptno = dept.deptno
    can be used directly in the select clause field direct access to data, but the efficiency is relatively low. With the table name before the proposed field
    Note: If the field is public, you must declare the table name
    select ename, job, sal, dname from emp, dept where emp.deptno = dept.deptno-- equivalent connection filtering
    select emp.ename, emp.job, emp.sal, dept.dname, emp.deptno from EMP, Dept WHERE emp.deptno = dept.deptno
    SELECT e.ename, e.job, e.sal, d.dname from EMP E, D WHERE Dept e.deptno = d.deptno and sal> 2000-- to the table using an alias
3, not equijoins
  query employee name, job, salary, wage levels
  * E emp from the SELECT, the WHERE e.sal salgrade S> = s.losal and e.sal <= s.hisal
4, since the connection (to do with himself Cartesian product)
  query employee name, job, salary, and superiors name
  SELECT e1.ename, e1.job, e1.sal, e2.ename EMP from E1, E2 EMP WHERE e1.mgr = e2.empno
. 5, the outer connecting
  left outer join: add on the right, left no corresponding field value data
    query employee name, work, salary, department name and employee information is no sector
    select * from emp e, dept d where e.deptno = d.deptno (+)
  right outer connection: adding to the left, to the right of the display field corresponding to a value no data
    query employee name, job, salary, department name and no staff information department
    select * from emp e, dept d where e.deptno (+) = d.deptno

SQL99 way

Note 1: still be added to the table alias
Note 2: If the key on usering or filter the results, must use the connection table and the inner join action table can be omitted where the inner
Note 3: outer connection may be omitted to keywords do not write
Note 4: still can continue to use grouping, having, sorting
1, Cartesian product: using cross join keyword
  select content from table cross join
  select * from emp cross join the dept
2, screening
  query employee name, job, salary , department name
  1) natural connection: use the keyword natural join
    use: select content from table natural join table name
    features 1: underlying Cartesian product first, and then automatically contour screened in accordance with all of the same name with the value of the field.
    Question 1: If you want to filter some of the fields in accordance with the results of how to do?
    Question 2: If you want to follow a different field names, but the same value equating screened how to do?
    select * from emp natural join dept
    solve 1: using keywords to
      action 1: indicates that the specified field on the results of the joint inquiry conducted equivalence screening
      Note: The indicated field must be the same name as a table with two value fields
      use: select content from table inner join table names using (field names, field names, ....)
      select * from emp inner join dept using (deptno)
    to solve 2: Use the keyword on custom connection criteria to filter (equivalent screening, screening is not equivalent)
      Note: Filters where ordinary filter, do not use on conduct. Benefits: readability of the SQL statement becomes stronger.
      Use: select content from table inner join table name on the connection conditions where ordinary Filters
      the SELECT * from emp inner join the dept on emp.deptno = dept.deptno where SAL> 2000
  2) outer join:
    left outer join: select content from table were left outer join table on the join condition
      query employee name, job, salary, department name and no department employee information
      select * from emp e left outer join dept d on e.deptno = d.deptno
    right outer join: select content from table right outer join table on the join condition
      query employee name, job, salary, department name and no department employee information
      select * from emp e right outer join dept d on e.deptno = d.deptno
    full outer join: select content from table full outer join table on the join condition
      * Full E emp from the SELECT Outer ON e.deptno the Join the dept d = d.deptno
  3) self-join:
    Queries employees and their superiors name
    . select e1 *, e2.ename from emp e1 inner join emp e2 on e1.mgr = e2.empno

Three tables joint inquiry

SQL92 achieve

Features: easy to write, difficult to read
disadvantages: SQL statement structure 92 is not clear
Usage:
  SELECT content (alias, connector, deduplication, oracle functions, logic operations)
  from table 1, table 2, table 3 ...
  where condition (connected condition, ordinary filter condition, where clause keywords)
  Group by group field
  having multi-line function screening
  order by sort field
query employee information and the name of the department and the name of the city and wages greater than 2000 or have the bonus
  select e . *, d.dname, c.cname
  from EMP E, D Dept, City C
  WHERE (e.deptno = d.deptno and d.loc = c.cid and SAL> 2000) or (e.deptno = d.deptno d.loc = c.cid and COMM and IS not null)
  the Order by e.sal 

SQL99 achieve

Features: hard to write, easy to read
usage:
  the SELECT content from table 1
  inner join table 2
  ON connection conditions
  inner join table 3
  ON connection conditions
  where ordinary filter condition
  group by group
  having multi-line function screening
  order by sorting
query employee information and department name and the name of the city and more than 2,000 employees wage bonus or have
  the SELECT * from emp E
  Inner the Join the dept d
  ON e.deptno = d.deptno
  Inner city the Join c
  ON d.loc = c.cid
  the wHERE e.sal> 2000 e.comm IS not null or
  the Order by e.sal

Subqueries

When to use it: When a query filter condition is not clear, consider using sub-queries.
1, single-row subquery
2, multiple-row subqueries

Single-row subquery

When to use it: Screening without a clear need to execute a query, and the query result is the value of a field and only one
note: where clause allowed the query, the query is called a subquery
use: select content from table where field name comparison operator sub-query
  query than all employees "CLARK" high-wage employee information
  select * from emp where sal> ( select sal from emp where ename = 'CLARK')
  query name wages higher than the average wage and salary employees
  select ename, sal from emp where sal> (select avg (sal) from emp)
  queries and soctt belong to the same sector and lower wages than his employee data
  select * from emp where deptno = ( select deptno from emp where ename = 'SCOTT') and sal <(select sal from emp where ename = 'SCOTT')
  of the same query functions and scott, hired earlier employee information
  select * from emp where job = ( select job from emp where ename = 'SCOTT') and hiredate <( select hiredate from emp where ename = ' SCOTT')

Multiple-row subqueries

Use: results of the subquery only one field, but the field has n values, consider using multiple-row subqueries, in fact, using keywords
Keyword 1: any arbitrarily
    select content from table where field name any sub-query comparison operator
key word 2: all all
    select content from table where field name comparison operators all sub-query
keyword 3: in the presence of any representation, the any equivalent = 
    select content from table where field names in the sub-query  
    select content from table where the field name not in the sub-query  
  query wage higher than any of the CLERK of all employee information
  select * from emp where sal> any (select sal from emp where job = 'CLERK')
  query SALESMAN wages higher than all the employee information
  select * from emp where sal> all (select sal from emp where job = 'SALESMAN')
  query department 20 in the same employees work in the same department of 10 employees of the information
  the SELECT the job from emp the WHERE deptno = 10
  the SELECT * from emp the WHERE the job in (the SELECT the job from emp where deptno = 10) and deptno = 20
  select * from emp where job = any (select job from emp where deptno=10) and deptno=20

Guess you like

Origin www.cnblogs.com/sunny-sml/p/11980388.html
Recommended