Various SQL JOIN reference

10 10 2007

One of the trickiest things about learning SQL is mastering how various JOIN statements differ in the ways they combine data from multiple data tables. There are three types of joins: inner, outer, and cross. In addition, there are three types of outer joins: left, right, and full. It can be frustrating trying to keep them differentiated, so here’s a quick guide. All of the following examples involve joining the authors and publishers tables in the Pubs sample database included with SQL Server.

Inner Joins

In an inner join, records from two tables are combined and added to a query’s results only if the values of the joined fields meet certain specified criteria. If you use an inner join to combine the authors and publishers tables based on their city and state columns, the result would be a list of all authors who live in the same city as a publisher:

image

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors AS a INNER JOIN publishers AS p
ON a.city = p.city
AND a.state = p.state
ORDER BY a.au_lname ASC, a.au_fname ASC

Outer Joins

An outer join returns all rows from the joined tables whether or not there’s a matching row between them. The ON clause supplements the data rather than filtering it. The three types of outer joins, left, right, and full, indicate the main data’s source.

Left Outer Joins

When you use a left outer join to combine two tables, all the rows from the left-hand table are included in the results. So, for the authors and publishers tables, the result will include a list of all authors along with a publisher’s name column. If a publisher exists in the author’s city, it’s listed. Otherwise, the field in the publisher’s column is set to NULL:

image

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

Right Outer Joins

A right outer join is conceptually the same as a left outer join except that all the rows from the right-hand table are included in the results. They can be included more than once, if more than one author exists in the publisher’s city. If no author lives in the publisher’s city, the author name fields are set to NULL:

image

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

Full Outer Join

As you might have gathered, a full outer join retrieves all the rows from both joined tables. It returns all of the paired rows where the join condition is true, plus the unpaired rows from each table concatenated with NULL rows from the other table. You usually won’t want to use one of these.

Cross Join

A cross join returns not the sum but the product of two tables. Each row in the left-hand table is matched up with each row in the right-hand table. It’s the set of all possible row combinations, without any filtering, as shown here:

USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
ORDER BY au_lname DESC

The resultset contains 184 rows (authors has 23 rows, and publishers has 8; therefore, 23 × 8 = 184). The first 11 rows look like this:

image

However, if you add a WHERE clause (like WHERE authors.city = publishers.city), a cross join functions as an inner join—it uses the condition to filter all possible row combinations down to the ones you want:

image

USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
WHERE authors.city = publishers.city
ORDER BY au_lname DESC

Compiled from TechNet.



OS X Leopard finalized

10 10 2007

imageNice!  A source tells AppleInsider that Mac OS X 10.5 Leopard is now “finalized,”  and that Apple has begun to provide Leopard-related support training materials to its support staff.

Go here for details.  And if you want to read more about the new features, go here.



Cool Software Digg-like

9 10 2007

Intel apparently just launched a Digg-like site where users can rate software startups/companies.  Interesting…

The CoolSW site is an online community of people passionate about software. Members can post information about an interesting new software company and, more important, the community votes on whether they think a software company is, well, “cool.” Companies that receive a lot of votes get elevated to the site’s front page where the casual visitor can see what community members consider the most interesting software.



Check in your SharePoint Images

5 10 2007

I wrote up an internal how-to/policy for the company I work for, which had text and some images that weren’t showing up. Googling and looking through the options for the document didn’t help.

After going through it some more, I found that you need to check in all items [in the Document Center] before it becomes viewable by others. What threw me off was that my department could see it, but not other departments.

The Document Check In Page followed with this message:

Other users will not see your changes until you check in. Specify options for checking in this document.

image

Hope this tip helps.



How HDMI works

2 10 2007

imageThe High-Definition Multimedia Interface (HDMI) is a licensable audio/video connector interface for transmitting uncompressed, encrypted digital streams.  HDMI is more than a port on the back of a TV (and the often expensive cable that fits inside). It’s a set of rules for allowing high-definition electronic devices to communicate.

 Read more about it here.