Beruflich Dokumente
Kultur Dokumente
An inner join is a join in which the values in the columns being joined are compared using a comparison operator. In the SQL-92 standard, inner joins can be specified in either the FROM or WHERE clause. This is the only type of join that SQL-92 supports in the WHERE clause. Inner joins specified in the WHERE clause are known as old-style inner joins. This Transact-SQL query is an example of an inner join: USE pubs SELECT * FROM authors AS a INNER JOIN publishers AS p ON a.city = p.city ORDER BY a.au_lname DESC This inner join is known as an equi-join. It returns all the columns in both tables, and returns only the rows for which there is an equal value in the join column. Here is the result set: au_id ----------238-95-7766 409-56-7008 state ----CA CA zip ----94705 94705 au_lname -------Carson Bennet contract -------1 1 au_fname -------Cheryl Abraham pub_id -----1389 1389 phone -----------415 548-7723 415 658-9932 address city --------------- -------589 Darwin Ln. Berkeley 6223 Bateman St. Berkeley city -------Berkeley Berkeley state ----CA CA country ------USA USA
(2 row(s) affected) In the result set, the city column appears twice. Because there is no point in repeating the same information, one of these two identical columns can be eliminated by changing the select list. The result is called a natural join. You can restate the preceding Transact-SQL query to form a natural join. For example: USE pubs SELECT p.pub_id, p.pub_name, p.state, a.* FROM publishers p INNER JOIN authors a ON p.city = a.city ORDER BY a.au_lname ASC, a.au_fname ASC Here is the result set: pub_id pub_name ------ --------------Algodata Infosystems CA state au_id au_lname au_fname -------- ----------- -------- -------- 1389 409-56-7008 Bennet Abraham
1389
Algodata Infosystems
CA
Carson
Cheryl
phone address --------------- ------------415 658-9932 6223 Bateman St. 415 548-7723 589 Darwin Ln. (2 row(s) affected)
contract --------1 1
(6 row(s) affected)
Note : The expression NOT column_name = column_name is equivalent to column_name < > column_name . This Transact-SQL example uses a not-equal join combined with a self-join to find all rows in the titleauthor table in which two or more rows have the same title_id but different au_id numbers (that is, books with more than one author): USE pubs SELECT DISTINCT t1.au_id, t1.title_id FROM titleauthor t1 INNER JOIN titleauthor t2 ON t1.title_id = t2.title_id WHERE t1.au_id <> t2.au_id ORDER BY t1.au_id Here is the result set: au_id ----------213-46-8915 267-41-2394 267-41-2394 409-56-7008 427-17-2319 472-27-2349 672-71-3249 722-51-5454 724-80-9391 724-80-9391 756-30-7391 846-92-7186 899-46-2035 899-46-2035 998-72-3567 title_id -------BU1032 BU1111 TC7777 BU1032 PC8888 TC7777 TC7777 MC3021 BU1111 PS1372 PS1372 PC8888 MC3021 PS2091 PS2091
SQL Server supports both the SQL-92 outer join syntax and a legacy syntax for specifying outer joins based on using the *= and =* operators in the WHERE clause. The SQL-92 syntax is recommended because it is not subject to the ambiguity that sometimes results from the legacy Transact-SQL outer joins.
The LEFT OUTER JOIN includes all rows in the authors table in the results, whether or not there is a match on the city column in the publishers table. Notice that in the results there is no matching data for most of the authors listed; therefore, these rows contain null values in the pub_name column.
(null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior Variations (null) (null) Cooking with Computers: Surreptitious Balance Sheets (null) (null) Emotional Security: A New Algorithm (null) (null) Fifty Years in Buckingham Palace Kitchens 7066 75 Is Anger the Enemy? (null) (null) Life Without Fear (null) (null) Net Etiquette (null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean (null) (null) Prolonged Data Deprivation: Four Case Studies (null) (null) Secrets of Silicon Valley (null) (null) Silicon Valley Gastronomic Treats (null) (null) Straight Talk About Computers (null) (null) Sushi, Anyone? (null) (null) The Busy Executive's Database Guide (null) (null) The Gourmet Microwave (null) (null) The Psychology of Computer Cooking (null) (null) You Can Combat Computer Stress! (18 row(s) affected) For more information about predicates, see WHERE.
Marjorie Morningstar Burt Sheryl Livia Charlene Stearns Heather Michael Sylvia Albert Anne Meander Dean Dirk Johnson Akiko Abraham Cheryl NULL NULL NULL NULL NULL NULL NULL (30 row(s) affected)
Green Greene Gringlesby Hunter Karsen Locksley MacFeather McBadden O'Leary Panteley Ringer Ringer Smith Straight Stringer White Yokomoto Bennet Carson NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL Algodata Infosystems Algodata Infosystems Binnet & Hardley Five Lakes Publishing GGG&G Lucerne Publishing New Moon Books Ramona Publishers Scootney Books
USE pubs SELECT au_fname, au_lname, pub_name FROM authors INNER JOIN publishers ON authors.city = publishers.city ORDER BY au_lname DESC
Using Self-Joins
A table can be joined to itself in a self-join. For example, you can use a self-join to find out the authors in Oakland, California who live in the same ZIP Code area. Because this query involves a join of the authors table with itself, the authors table appears in two roles. To distinguish these roles, you must give the authors table two different aliases (au1 and au2) in the FROM clause. These aliases are used to qualify the column names in the rest of the query. This is an example of the self-join Transact-SQL statement: USE pubs SELECT au1.au_fname, au1.au_lname, au2.au_fname, au2.au_lname FROM authors au1 INNER JOIN authors au2 ON au1.zip = au2.zip WHERE au1.city = 'Oakland' ORDER BY au1.au_fname ASC, au1.au_lname ASC Here is the result set: au_fname -------------------Dean Dean Dean Dirk Dirk Dirk Livia Livia Livia Marjorie Stearns (11 row(s) affected) To eliminate the rows in the results in which the authors match themselves and to eliminate rows that are identical, except the order of the authors is reversed, make this change to the Transact-SQL self-join query: USE pubs SELECT au1.au_fname, au1.au_lname, au2.au_fname, au2.au_lname FROM authors au1 INNER JOIN authors au2 ON au1.zip = au2.zip WHERE au1.city = 'Oakland' AND au1.state = 'CA' AND au1.au_id < au2.au_id au_lname ------------------Straight Straight Straight Stringer Stringer Stringer Karsen Karsen Karsen Green MacFeather au_fname -------------------Dean Dirk Livia Dean Dirk Livia Dean Dirk Livia Marjorie Stearns au_lname --------Straight Stringer Karsen Straight Stringer Karsen Straight Stringer Karsen Green MacFeather
ORDER BY au1.au_lname ASC, au1.au_fname ASC Here is the result set: au_fname -----------Dean Dean Dirk au_lname ----------------Straight Straight Stringer au_fname -------------------Dirk Livia Livia au_lname -------------------Stringer Karsen Karsen
(3 row(s) affected) It is now clear that Dean Straight, Dirk Stringer, and Livia Karsen all have the same ZIP Code and live in Oakland, California.
The middle table of the join (the titleauthor table) can be called the translation table or intermediate table, because titleauthor is an intermediate point of connection between the other tables involved in the join. When there is more than one join operator in the same statement, either to join more than two tables or to join more than two pairs of columns, the join expressions can be connected with AND or with OR.