Semi-join anti-join

Source: Internet
Author: User
Tags dname

Table join methods include join, semi-join, outer-join, and anti-join;

Table join implementation methods such as nested loop, merge, and hash.

This article briefly introduces table join, semi-join, outer-join, anti-join, and applicable scenarios.

Assume that two data sources (row
Source ).

EMP (id pk, ename, deptno) dept (deptno PK, dname)

Join

Select ename, dname from EMP, DEPT where EMP. deptno = dname. deptno;

Two data source key values are compared one by one, and a matching record set is returned.

For example: nested loop join

for x in ( select * from emp )
   loop
       for y in ( select * from dept)
           loop
               if ( x.deptno == y.deptno )
                   OutPut_Record(x.ename,y.dname)
               End if
           end loop

End Loop

Outer-join

Select ename, dname from EMP, DEPT where EMP. deptno = Dept. deptno (+ );

Select ename, dname from EMP, DEPT where EMP. deptno (+) = Dept. deptno;

The key values of the two data sources are compared one by one, and matched results are returned. However, if no match is found in the other row source, a record is returned.

For example: nested loop outer-join

for x in ( select * from emp )
   loop
       find_flag=false;
       for y in ( select * from dept)
           loop
               if ( x.deptno == y.deptno )
                   OutPut_Record(x.ename,y.dname)
                   Find_flag=true
               End if
           end loop
       if ( find_flag == false )
           OutPut_Record(x.ename,null)
       End if 

End Loop

Semi-join

Select dname from Dept where exists (select null from EMP where EMP. deptno = Dept. deptno)

Mostly used in the subquery exists, for external row
Each key value of source,Find internalReturns the first key value matched by row source.If it is found, you do not need to find the internal row.
Other source key values.

For example: nested loop semi-join

for x in ( select * from dept )
   loop
       for y in ( select * from emp)
           loop
               if ( x.deptno == y.deptno )
                   OutPut_Record(x.dname)
                   Break;

               End if
           end loop

End Loop

Anti-join

Select ename, deptno from EMP, DEPT where EMP. deptno! = Dept. deptno

Mostly used! = Not in and other queries; if the conditions are found (! = Not
In) does not return, does not meet the condition (! = Not in. Is opposite to join.

For example: nested loop anti-join

for x in ( select * from emp )
   loop
       for y in ( select * from dept)
           loop
               if ( x.deptno != y.deptno )
                   OutPut_Record(x.dname,y.deptno)
               End if
           end loop

End Loop

More information get document

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.