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.
Is the row matched by the join column. If a row in the left table does not match a row in the right table
All columns in the right table 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 is in the left table
If no matching row exists, 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. When a row does not match in another table
The selection list column of a table contains null values. If there are matched rows between tables, the entire result set row contains the data of the base table.
Value.
Rows are returned only when at least one row in the same two tables meets the join conditions. Internal join elimination and other
Any row in a table does not match. The outer join will return at least one table or
All rows in the view, as long as these rows meet any where or having search conditions. Retrieve
All rows in the left table referenced from the left Outer Join and all rows in the right table referenced by the right outer join. Complete
All rows of the two tables in the join operation are returned.
Microsoft SQL Server 2000
Join uses the following SQL-92 keywords:
Left Outer Join or left join
Right Outer Join or right join
Full outer join or full join
SQL Server supports the Outer Join syntax for the SQL-92 and the use
* = And = * operator specifies the old Syntax of Outer Join. Since SQL-92 syntax is not prone to discrimination
Meaning, while the old Transact-SQL Outer Join sometimes produces ambiguity, so it is recommended to use the SQL-92 Language
Method.
Use left Outer Join
Assume that the authors table and the publishers table are joined in the city column. The result is only displayed in
Author of the publisher's city (in this example, Abraham Bennet and Cheryl Carson)
).
To include all the authors in the results, regardless of whether the publisher lives in the same city, use SQL-
92 left Outer Join. 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 Karsen 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
All rows of the authors table are included in the result. Note: Most of the authors listed in the results do not have phase
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 result is only displayed in
Author of the publisher's city (in this example, Abraham Bennet and Cheryl Carson)
). The SQL-92 right Outer Join operator right Outer Join specifies that no matter the first table
If any matched data exists, the result will contain all rows in the second table.
Use
The SQL-92 is connected right outward. 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 null binnet & hardley
Null null five lakes Publishing
Null null ggg & G
Null null Lucerne Publishing
Null null New Moon books
Null null Ramona publishers
Null 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
Answer, but eliminate the title of books with sales 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 provides the full external join operator full
Outer Join, regardless of whether the other table has matched values, 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 result is only displayed in
Author of the publisher's city (in this example, Abraham Bennet and Cheryl Carson)
). The SQL-92 full outer join operator specifies that
The result will contain all rows in the two tables.
To include all authors and publishers in the results, whether or not there are publishers or publishers in the city
In the same city, use a complete external connection. The following is a query of the Complete External join of transact-SQL.
And result:
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 Karsen 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 null binnet & hardley
Null null five lakes Publishing
Null null ggg & G
Null null Lucerne Publishing
Null null New Moon books
Null null Ramona publishers
Null null scootney books
(30 row (s) affected)