Four types of connections for SQL-left outer, right outer, internal, fully connected

Source: Internet
Author: User
Tags joins

The join condition can be specified in the From or WHERE clause, and it is recommended that the join condition be specified in the FROM clause. The WHERE and having clauses can also contain search conditions to further filter the rows selected by the join condition.
Joins can be divided into the following categories:


1, INNER join (typical join operation, using a comparison operator like = or <>). Includes equality joins and natural joins.
Inner joins use comparison operators to match rows in two tables based on the values of the columns that are common to each table. For example, retrieve all lines of the students and courses table with the same student identification number.

2, outer joins . An outer join can be a left outer join, a right outer join, or a full outer join.
When you specify an outer join in the FROM clause, you can specify it by one of the following sets of keywords:

1) left JOIN or left OUTER join
The result set of the left outer join includes all rows of the left table specified in the outer clause, not just the rows that match the joined columns. If a row in the left table does not have a matching row in the right table, all select list columns in the right table in the associated result set row are null values.

2) Right  join or  outer  JOIN     
A LEFT outer join is a reverse join of a leftist join. All rows of the right table will be returned. If a row in the right table does not have a matching row in the left table, a null value will be returned for left table.        
3) Full  join or fully OUTER join
Complete outer joins return all rows from the left and right tables. When a row does not have a matching row in another table, the selection list column for the other table contains a null value. If there are matching rows between the tables, the entire result set row contains the data values of the base table.    
    
3, cross join     
Cross join returns all rows in the left table. Each row in the left table is combined with all rows in the right table. Cross joins are also called Cartesian product.      The table or view in the FROM clause of the

can be specified in any order by an inner join or a full outer join, but the order of the table or view is important when you specify a table or view with a left or right outer join. For more information about using left or right outward joins to arrange tables, see Using outer joins.      
    
Example:   

-------------------------------------------------
A table ID name B table ID job parent_id
1 Sheets 3 1 23 1
2 Lee 42 34 2
3 Wang Wu 3 34 4
Relationship between a.ID and parent_id

--------------------------------------------------
1) Internal connection
Select a.*,b.* from a inner join B on a.id=b.parent_id
The result is
1 Sheets 3 1 23 1
2 Lee 42 34 2

2) Left Connection
Select a.*,b.* from a LEFT join B on a.id=b.parent_id
The result is
1 Sheets 3 1 23 1
2 Lee 42 34 2
3 Wang Wu Null

 3)   right connection   
  Select   a.*,b.*   from   a   right   join   B & nbsp   on   a.id=b.parent_id       
  results     
  1   3                   1         1   
  2   John Doe                  2         2   < br>  null                       3       &NBS P 4   
    
 4)   full connection    
  Select   a.*,b.*   From   A   full   joins   B     on   a.id=b.parent_id   

The result is
1 Sheets 3 1 23 1
2 Lee 42 34 2
Null 3 34 4
3 Wang Wu Null

Four types of connections for SQL-left outer, right outer, internal, fully connected

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.