A multi-table operation that is tied into SQL

Source: Internet
Author: User

One, multi-table query when null value processing
    • Ask to return all employees with lower wages than "Allen"

  Select A.ename,a.conn from emp a WHERE A.conn < (select b.conn from emp b where b.ename = ' Allen ');

    • The correct wording should be

  Select A.ename,a.conn from EMP a where COALESCE (a.conn,0) < (select B.conn from emp b from b.ename = ' Allen ');

    • Watch out.

A null value compares the result or null, so a conversion of null values is required here.

Ii. Aggregation and external links
    • Require return of wages and bonuses for all departments

Select E.deptno,

SUM (e.sal) as Total_sal,

SUM (e.sal*eb2.rount) as Total_bonus,

From EMP

Left join (select Eb.empno,

SUM (case if type =1 then 0.1

When type =2 then 0.2

When type =3 then 0.3 end) as Tote

From Emp_bonux EB

Group by Eb.empno) eb2 on eb2.empno = E.deptno

GROUP BY E.deptno

oder by 1;

    • Multiple tables return lost data with full JOIN
Iii. Aggregation and internal connections
    • Request return Department for 10 of all employees wages and bonuses

Select E.deptno

E.empno,

E.ename,

(E.sal * Case if type =1 then 0.1

When type =2 then 0.2

When type =3 then 0.3 end) as bonus

From EMP E

Inner JOIN emp_bonus eb on eb.empno = Eb.empno

where D.deptno = 10

ORDER BY

  If you aggregate, you must first summarize the bonus according to the employee and then assemble it.

  Select E.deptno

SUM (e.sal) as Total_sal,

SUM (e.sal*eb.bonus) as Total_bonus

From EMP E

INNER JOIN (select Eb.empno,

Case if Type =1 then 0.1

When type =1 then 0.2

When type =1 then 0.3 end) as bonus

Group by Eb.empno) eb2 on eb2.empno = E.empno

where E.deptno = 10;

Group BY E.deptno;

Iv. UNION ALL, union, and empty string

Select Ename as Department name, Denpno department number, EVL (MGR,DEPTNO) as superior code

Nnion All

Select Ename as Department name, denpno department number, null as ancestor code (should use ' empty string ')

    • Unique identification must be added when duplicate data is available

74990 30 74990 30

7521 30 7521 30

7654 30 7654 30

7844 30 7844 30

7900 30

These two data sitting aggregation operation will appear or there are 5 data and the Union will only appear a data solution is to join the unique field

Select Empno,deptno from emp where mgr = 7684

UNION

Select Empno,deptno from emp where job = ' saleman ' to ensure correct deduplication

Select Disdintc Deptno from (

Select Empno,deptno from emp where mgr = 7684

UNION All

Select Empno,deptno from emp where job = ' Saleman '

)

Order BY Deptno;

 

V. Combinations of related rows
    • Show employee code, name, department name, and work address for department 10

Select E.deptno,e.ename,e.dname,e.loc

From EMP E

INNER JOIN Dept D on (e.deptno = D.deptno)

where E.deptno = 10

Select E.empno,e.ename,d.dname,d.loc

From EMP e,dept D

E.deptno = D.deptno and E.deptno = 10

VI. combinations of related rows

(right Join) (INNER join) (full join)

Self-correlation can be queried with a LEFT join

 

   

A multi-table operation that is tied into SQL

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.