Full use of very detailed SQL--JOIN

Source: Internet
Author: User

Outer Join. Outer Join can be left Outer Join, right outer join, or complete external join.
When an external join is specified in the FROM clause, it can be specified by one of the following sets of keywords:

Left join or left outer join.
The result set of the left outer Join includes all rows in the LEFT table specified in the left outer clause, not just the rows matched by the join column. If a row in the left table does not match a row in the right table, all selection list columns in the right table in the row of the associated result set are null.

Right join or right outer join.
The right outer join is the reverse join of the left Outer Join. All rows in the right table are returned. If a row in the right table does not match a row in the left table, a null value is returned for the left table.

Full join or full outer join.
The Complete External Join Operation returns all rows in the left and right tables. If a row does not match a row in another table, the selection list column of the other table contains a null value. If there are matched rows between tables, the entire result set row contains the data value of the base table.

Rows are returned only when at least one row in the same two tables meets the join conditions. The inner join removes rows that do not match any row in the other table. The outer join will return all rows of at least one table or view mentioned in the FROM clause, as long as these rows meet any WHERE or HAVING search conditions. Searches all rows in the left table referenced by the left Outer Join and all rows in the right table referenced by the right outer join. All rows of the two tables in the complete external join will be returned.

Microsoft®SQL Server™2000 use the following SQL-92 keywords for outer joins specified in the FROM clause:

Left outer join or LEFT JOIN


Right outer join or RIGHT JOIN


Full outer join or FULL JOIN
SQL Server supports the SQL-92 Outer Join syntax and specifies the old syntax for outer join using the * = and = * operators in the WHERE clause. Since the SQL-92 syntax is not prone to ambiguity, while the legacy Transact-SQL Outer Join sometimes produces ambiguity, it is recommended to use the SQL-92 syntax.

Use left Outer Join
Assume that the authors table and the publishers table are joined in the city column. The results only show the author of the publisher's city (Abraham Bennet and Cheryl Carson in this example ).

To include all authors in the results, regardless of whether the publisher lives in the same city, use the SQL-92 to connect left out. The following are the query results of the left Outer Join of Transact-SQL:

USE pubs
SELECT a. au_fname, a. au_lname, p. pub_name
FROM authors a left outer join publishers p
ON a. city = p. city
Order by p. pub_name ASC, a. au_lname ASC, a. au_fname ASC

The following is the result set:

Au_fname au_lname pub_name
-------------------------------------------------------------------
Reginald Blotchet-Hils NULL
Micel DeFrance NULL
Innes del Castillo NULL
Ann Dull NULL
Marjorie Green NULL
Morningstar Greene NULL
Burt Gringlesby NULL
Sheryl Hunter NULL
Livia K *** n NULL
Charlene Locksley NULL
Stearns MacFeather NULL
Heather McBadden NULL
Michael O 'Leary NULL
Sylvia Panteley NULL
Albert Ringer NULL
Anne Ringer NULL
Meander Smith NULL
Dean Straight NULL
Dirk Stringer NULL
Johnson White NULL
Akiko Yokomoto NULL
Abraham Bennet Algodata Infosystems
Cheryl Carson Algodata Infosystems

(23 row (s) affected)

Whether or not it matches the city column in the publishers table, left outer join will contain all rows in the authors table in the result. Note: most authors in the results do not have matched data. Therefore, the pub_name column of these rows contains null values.

Use Right Outer Join
Assume that the authors table and the publishers table are joined in the city column. The results only show the author of the publisher's city (Abraham Bennet and Cheryl Carson in this example ). The SQL-92 right outer join operator right outer join specifies that the result contains all rows in the second table regardless of whether the first table has matched data.

To include all publishers in the results, regardless of whether there are publishers in the city, use the SQL-92 to connect right out. The following are the queries and results of the right Outer Join of Transact-SQL:

USE pubs
SELECT a. au_fname, a. au_lname, p. pub_name
FROM authors AS a right outer join publishers AS p
ON a. city = p. city
Order by p. pub_name ASC, a. au_lname ASC, a. au_fname ASC

The following is the result set:

Au_fname au_lname pub_name
----------------------------------------------------------------
Abraham Bennet Algodata Infosystems
Cheryl Carson Algodata Infosystems
NULL Binnet & Hardley
NULL Five Lakes Publishing
Null ggg & G
NULL Lucerne Publishing
NULL New Moon Books
NULL Ramona Publishers
NULL Scootney Books

(9 row (s) affected)

Using predicates (such as comparing joins with constants) can further limit outer joins. The following example contains the same right outer join, but removes the title of a book with sales volume less than 50:

USE pubs
SELECT s. stor_id, s. qty, t. title
FROM sales s right outer join titles t
ON s. title_id = t. title_id
AND s. qty> 50
Order by s. stor_id ASC

The following is the result set:

Stor_id qty title
----------------------------------------------------------------------
(Null) But Is It User Friendly?
(Null) Computer Phobic AND Non-Phobic Individuals: Behavior
Variations
(Null) Cooking with Computers: Surreptitious Balance Sheets
(Null) Emotional Security: A New Algorithm
(Null) Partition ty Years in Buckingham Palace Kitchens
7066 75 Is Anger the Enemy?
(Null) Life Without Fear
(Null) Net Etiquette
(Null) Onions, Leeks, and Garlic: Cooking Secrets of
Mediterranean
(Null) Prolonged Data Deprivation: Four Case Studies
(Null) Secrets of Silicon Valley
(Null) Silicon Valley Gastronomic Treats
(Null) Straight Talk About Computers
(Null) Sushi, Anyone?
(Null) The Busy Executive's Database Guide
(Null) The Gourmet Microwave
(Null) The Psychology of Computer Cooking
(Null) You Can Combat Computer Stress!

(18 row (s) affected)

For more information about predicates, see WHERE.

Use a complete external connection
To retain the unmatched information by including unmatched rows in the join results, use the complete external join. Microsoft®SQL Server™2000 The Complete External JOIN operator full outer join is provided. no matter whether the other table has a matching value, this operator includes all rows in the two tables.

Assume that the authors table and the publishers table are joined in the city column. The results only show the author of the publisher's city (Abraham Bennet and Cheryl Carson in this example ). The SQL-92 full outer join operator specifies that results will include all rows in both tables regardless of whether the table has matched data.

To include all authors and publishers in the results, regardless of whether there are publishers or publishers in the city living in the same city, use a complete external connection. The following are the query results of the Complete External join of Transact-SQL:

USE pubs
SELECT a. au_fname, a. au_lname, p. pub_name
FROM authors a full outer join publishers p
ON a. city = p. city
Order by p. pub_name ASC, a. au_lname ASC, a. au_fname ASC

The following is the result set:

Au_fname au_lname pub_name
--------------------------------------------------------------------
Reginald Blotchet-Hils NULL
Micel DeFrance NULL
Innes del Castillo NULL
Ann Dull NULL
Marjorie Green NULL
Morningstar Greene NULL
Burt Gringlesby NULL
Sheryl Hunter NULL
Livia K *** n NULL
Charlene Locksley NULL
Stearns MacFeather NULL
Heather McBadden NULL
Michael O 'Leary NULL
Sylvia Panteley NULL
Albert Ringer NULL
Anne Ringer NULL
Meander Smith NULL
Dean Straight NULL
Dirk Stringer NULL
Johnson White NULL
Akiko Yokomoto NULL
Abraham Bennet Algodata Infosystems
Cheryl Carson Algodata Infosystems
NULL Binnet & Hardley
NULL Five Lakes Publishing
Null ggg & G
NULL Lucerne Publishing
NULL New Moon Books
NULL Ramona Publishers
NULL Scootney Books

(30 row (s) affected)

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.