Four ways to connect in SQL

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/afirefly/archive/2010/10/08/1845906.html

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 right OUTER join
A right outer join is a reverse join of a left outer 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 full OUTER join
A full outer join returns 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
A cross join returns all the rows in the left table, with each row in the left table combined with all the rows in the right table. Cross joins are also called Cartesian product .

The table or view in the FROM clause 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.

There are two ways to use a connection in a database: Using the JOIN keyword, or using (+) this way
Example:

-------------------------------------------------
  A table     ID   name       & nbsp   b     ID   job   parent_id   
              1   3                      1     to     1 & NBSP;&NBSP
              2   John Doe             &NBS P       2     &NBSP;&NBSP     2,
              3.------   Wang Wu                     3     $     4        
  a.id with parent_id   existence relationship    

--------------------------------------------------
1) Internal connection
Select a.*,b.* from a inner join B on a.id=b.parent_id

or is:

Select a.*,b.* from a b where 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

or a

Select a.*,b.* from a b where 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 on a.id=b.parent_id

or a

Select a.*,b.* from A, b where a.id (+) =b.parent_id

     
  results   &NBSP;&NBSP
  1   3           &N Bsp       1     John Doe     1   
  2           &NB Sp        2     &NBS     2   
  NULL         P             3     &NBSP;&NBSP     4   
 
 ----- 4)   fully connected     
  Select   a.*,b.*   from   a   full   j Oin   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 ways to connect in 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.