Friday, March 18, 2011

Right Outer Join

Right Outer Joins are the logical opposite of Left Outer Joins. Right Outer Joins return all records from the 'right' table even if there is no record match in the 'left' table.


Look at this:
SELECT <COLUMNS>
FROM <TABLE1>RIGHT 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: RIGHT JOIN is the same as RIGHT OUTER JOIN


Here is an example:


  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. INSERT INTO @Passwords
  35. VALUES (10, 'bad password!');
  36. -- Right outer join between two table variables.
  37. SELECT U.firstName, U.lastName, P.[password]
  38. FROM @UserAccounts AS U RIGHT OUTER JOIN @Passwords AS P
  39. ON U.acountID = P.acountID;

The results of this query are:


(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)

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

(4 row(s) affected)

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)

Thursday, March 17, 2011

Table Variables and Inner Joins

When we create and use table variables, we sometimes need to user inner joins on table variables. Well, thankfully, this is the same as joining two regular tables:

  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 some records into our Passwords table variable.
  24. INSERT INTO @Passwords
  25. VALUES (1, 'password');
  26. INSERT INTO @Passwords
  27. VALUES (2, 'jane32');
  28. INSERT INTO @Passwords
  29. VALUES (3, 'Password!');
  30. -- Inner join between two table variables.
  31. SELECT U.firstName, U.lastName, P.[password]
  32. FROM @UserAccounts AS U
  33. INNER JOIN @Passwords AS P
  34. ON U.acountID = P.acountID;

The output of this query should be:


(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!

(3 row(s) affected)

Wednesday, March 16, 2011

Updating Records And Table Variables

In a previous post, located here, we talked about inserting records into table variables. Then in this post we talked about deleting records from a table variable. Now I will illustrate updating records within a table variable. It is the same as updating records of normal tables, using an "UPDATE" statement:

  1. USE master;
  2. GO
  3. -- The creation of our Table Variable.
  4. DECLARE @UserAccounts TABLE
  5. (
  6.     firstName VARCHAR(50)
  7.     , lastName VARCHAR(50)
  8. );
  9. -- Inserting 3 records into our Table Variable.
  10. INSERT INTO @UserAccounts
  11. VALUES ('John', 'Doe');
  12. INSERT INTO @UserAccounts
  13. VALUES ('Jane', 'Doe');
  14. INSERT INTO @UserAccounts
  15. VALUES ('Jim', 'Doe');
  16. -- Update a record from our Table Variable.
  17. UPDATE @UserAccounts
  18. SET firstName = 'This is an unrealistic name.'
  19. WHERE firstName = 'Jim';
  20. -- Selecting the records from our Table Variable.
  21. SELECT * FROM @UserAccounts;

The result of this query should be:


(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
firstName                                          lastName
-------------------------------------------------- --------------------------------------------------
John                                               Doe
Jane                                               Doe
This is an unrealistic name.                       Doe

(3 row(s) affected)

Deleting Records And Table Variables

In this post I talked about the creation of a table variable and then inserting records into our table variable. At times we will need to remove records from our table variable, and the way to do this is just like removing records from normal tables using the "DELTE" statement:

  1. USE master;
  2. GO
  3. -- The creation of our Table Variable.
  4. DECLARE @UserAccounts TABLE
  5. (
  6.     firstName VARCHAR(50)
  7.     , lastName VARCHAR(50)
  8. );
  9. -- Inserting 3 records into our Table Variable.
  10. INSERT INTO @UserAccounts
  11. VALUES ('John', 'Doe');
  12. INSERT INTO @UserAccounts
  13. VALUES ('Jane', 'Doe');
  14. INSERT INTO @UserAccounts
  15. VALUES ('Jim', 'Doe');
  16. -- Delete a record from our Table Variable.
  17. DELETE FROM @UserAccounts
  18. WHERE firstName = 'John';
  19. -- Selecting the records from our Table Variable.
  20. SELECT * FROM @UserAccounts;

The output of this query should be:



(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
firstName                                          lastName
-------------------------------------------------- --------------------------------------------------
Jane                                               Doe
Jim                                                Doe

(2 row(s) affected)

Inserting Records With Table Variables

In a previous post, located here, I talked about using table variables with SQL Server. This post will show the basic insertion of records into a table variable we will created called "UserAccounts". The fields will be "firstName" and "lastName". Then, we will select all the records from our table variable.

  1. USE master;
  2. GO
  3. -- The creation of our Table Variable.
  4. DECLARE @UserAccounts TABLE
  5. (
  6.     firstName VARCHAR(50)
  7.     , lastName VARCHAR(50)
  8. );
  9. -- Inserting 3 records into our Table Variable.
  10. INSERT INTO @UserAccounts
  11. VALUES ('John', 'Doe');
  12. INSERT INTO @UserAccounts
  13. VALUES ('Jane', 'Doe');
  14. INSERT INTO @UserAccounts
  15. VALUES ('Jim', 'Doe');
  16. -- Selecting the records from our Table Variable.
  17. SELECT * FROM @UserAccounts;

The output of running this query should be:



(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

firstName                                          lastName
-------------------------------------------------- --------------------------------------------------
John                                               Doe
Jane                                               Doe
Jim                                                Doe

(3 row(s) affected)

Thursday, March 3, 2011

Resources

Check back to this post often as it will be updated and improved over time.


Microsoft


http://msdn.microsoft.com/en-us/ - The Microsoft MSDN website. Provides articles, whitepapers, interviews, and sample code for software developers using Microsoft products.


http://www.asp.net/ - The Official ASP.NET website. Official site maintained by Microsoft with links to information, IBuySpy and other community sites and resources


http://www.microsoft.com/net/ - Microsoft .NET Framework website.


http://msdn.microsoft.com/en-us/vbasic/default - Visual Basic Developer Center


http://msdn.microsoft.com/en-us/vcsharp/default - Visual C# Developer Center


http://windowsclient.net/default.aspx - The Official Microsoft WPF and Windows Forms Site


https://www.dreamspark.com/default.aspx - Microsoft DreamSpark - Microsoft DreamSpark is a platform for downloading free softwares for students.




JavaScript and jQuery


http://jquery.com/ - jQuery - The Write Less, Do More, JavaScript Library




Online Communities


http://dreamincode.net/ - DreamInCode.net - Programming and Web Development Help
I Break Code Where code gets done.
ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |