Friday, March 18, 2011

Left Outer Join

Left Joins are cool in that fact that they return all records from the 'left' table and all the records from the 'right' table that have a match with a record from the 'left' table. If there is no match found, the query will place a 'null' value where there was no match found.

Left table? Right table? What?! If the idea of a left and right table is confusing to you, do not be discouraged. It is not too horrible to figure out. The left table is generally the table listed first, and you guessed it, the right table is generally the table listed second.

Look at this:
SELECT <COLUMNS>
FROM <TABLE1>LEFT JOIN <TABLE2>
ON <TABLE1>.<COLUMN> = <TABLE2>.<COLUMN>


In the above sample, <TABLE1> is the left table and <TABLE2> is the right table.


In this line:


     ON <TABLE1>.<COLUMN> = <TABLE2>.<COLUMN>


<COLUMN> is the name of the column that is similar, the column that will be used for matching.


Note: LEFT JOIN is the same as LEFT OUTER JOIN


And here is an example that you can run yourself to see the results:


  1. USE master;
  2. GO
  3. -- Create our first table variable to hold user accounts.
  4. DECLARE @UserAccounts TABLE
  5. (
  6.     acountID INT
  7.     , firstName VARCHAR(50)
  8.     , lastName VARCHAR(50)
  9. );
  10. -- Create our second table variable to hold our user account passwords.
  11. DECLARE @Passwords TABLE
  12. (
  13.     acountID INT
  14.     , [password] VARCHAR(50)
  15. );
  16. -- Insert some records into our UserAccounts table variable.
  17. INSERT INTO @UserAccounts
  18. VALUES (1, 'John', 'Doe');
  19. INSERT INTO @UserAccounts
  20. VALUES (2, 'Jane', 'Doe');
  21. INSERT INTO @UserAccounts
  22. VALUES (3, 'Jim', 'Doe');
  23. INSERT INTO @UserAccounts
  24. VALUES (4, 'Jimmy', 'Doe');
  25. INSERT INTO @UserAccounts
  26. VALUES (5, 'Fonda', 'Doe');
  27. -- Insert some records into our Passwords table variable.
  28. INSERT INTO @Passwords
  29. VALUES (1, 'password');
  30. INSERT INTO @Passwords
  31. VALUES (2, 'jane32');
  32. INSERT INTO @Passwords
  33. VALUES (3, 'Password!');
  34. -- Left outer join between two table variables.
  35. SELECT U.firstName, U.lastName, P.[password]
  36. FROM @UserAccounts AS U LEFT OUTER JOIN @Passwords AS P
  37. ON U.acountID = P.acountID;

This query will produce the following result:



(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
firstName                                          lastName                                           password
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
John                                               Doe                                                password
Jane                                               Doe                                                jane32
Jim                                                Doe                                                Password!
Jimmy                                              Doe                                                NULL
Fonda                                              Doe                                                NULL

(5 row(s) affected)

No comments:

Post a Comment

I Break Code Where code gets done.
ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |