Join Operation (learning notes) and Join Operation learning notes

Source: Internet
Author: User

Join Operation (learning notes) and Join Operation learning notes

There are two connection operations for data tables in the database:

Internal Connection:It is also said that the condition for equi join to eliminate Cartesian Product in where is that the internal join method is used.

External Connection:Only the conditions that meet the equivalence conditions can be displayed in the internal join. If you want to display all the data in a specific table, you must use an external join.

There are three types of external connections

Use (+) in Oracle to indicate connection

  • Example 1 (internal join)

Joint query of emp and dept tables

 

SELECT * FROM EMP E,DEPT DWHERE E.DEPTNO=D.DEPTNO;

 

 

  • Example 2 (Right Outer Join)

Add a right outer connection to display information about 40 departments in the department table

SELECT * FROM EMP E,DEPT DWHERE E.DEPTNO(+)=D.DEPTNO;
  • Example 3 (left Outer Join)

 

First, insert a record with no department number in the emp table.

INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)VALUES(9999,'test','CLERK',7369,SYSDATE,800,100,NULL);

The left Outer Join shows the information with ID 9999.

SELECT * FROM emp e,dept dWHERE e.deptno=d.deptno(+);

Self-Association:

  • Example 4

The employee ID, name, and superior leader ID and name are displayed.

SELECT e. empno, e. ename, e. mgr emp table leader number, m. lead number in the empno m Table, m. enameFROM emp e, emp mWHERE e. mgr = m. empno (+ );
  • Example 5

Query the number, name, start date (year-month-day display), position, lead name, Employee monthly salary sal, and total annual salary (sal + comm) of all employees who have joined 1981) * 12. salary grade, department ID, Department name, Department location, and county. The monthly salary of these employees is required to be between 1500 and ~ 3500. Sort the final results in descending order of the total annual salary. If the annual salary is equal, the results are sorted in ascending order of the employment time.

SELECT e. empno, e. ename, to_char (e. hiredate, 'yyyy-mm-dd') employment date, e. job, e. sal basic monthly salary, (e. sal + NVL (e. comm, 0) * 12 yearsal, m. ename name, DECODE (s. grade, 1, 'e and other employees', 2, 'd and other employees', 3, 'c and other employees', 4, 'B and other employees', 5, 'A and other wage ') wage level, d. deptno, d. dname, d. locFROM emp e, emp m, salgrade s, dept dWHERE to_char (e. hiredate, 'yyyy') = '000000' AND e. mgr = m. empno (+) AND e. sal BETWEEN s. losal AND s. hisal AND e. deptno = d. deptno (+) AND e. sal BETWEEN 1500 AND 3500 order by yearsal, e. hiredate ASC;

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.