Home > How to construct and use SQL OUTER JOINs optimally
Book Excerpt:
EMAIL THIS LICENSING & REPRINTS

How to construct and use SQL OUTER JOINs optimally

01 Mar 2008 | SearchSQLServer.com

Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google

Learn the basics of a SQL Server OUTER JOIN in this excerpt from "SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL, Second Edition." You'll discover how to construct LEFT, RIGHT and FULL OUTER JOINs with how-to examples through sample statements and tables. Authors John L. Viescas and Michael J. Hernandez also examine problems that may occur when using multiple OUTER JOINs and how to solve them.

What is an OUTER JOIN?

If you have any comments or questions about the information presented in this book excerpt, please send an email to editor@searchsqlserver.com.
As we explained in the previous chapter, the SQL Standard defines several types of JOIN operations to link two or more tables or result sets. An OUTER JOIN asks your database system to return not only the rows that match on the criteria you specify but also the unmatched rows from either one or both of the two sets you want to link.

Let's suppose, for example, that you want to fetch information from the School Scheduling database about students and the classes for which they're registered. As you learned in the previous chapter, an INNER JOIN returns only students who have registered for a class and classes for which a student has registered. It won't return any students who have been accepted at the school but haven't signed up for any classes yet, nor will it return any classes that are on the schedule but for which no student has yet shown an interest.

What if you want to list all students and the classes for which they are registered, if any? Conversely, suppose you want a list of all the classes and the students who have registered for those classes, if any. To solve this sort of problem, you need to ask for an OUTER JOIN.

Figure 9–1 uses a set diagram to show one possible relationship between students and classes. As you can see, a few students haven't registered for a class yet, and a few classes do not yet have any students signed up to take the class.


Figure 9–1 A possible relationship between students and classes

If you ask for all students and the classes for which they are registered, you'll get a result set resembling Figure 9–2.


Figure 9–2 All students and the classes for which they are registered

You might ask, "What will I see for the students who haven't registered for any classes?" If you remember the concept of a Null or "nothing" value discussed in Chapter 5, Getting More Than Simple Columns, you know what you'll see: When you ask for all students joined with any classes, your database system will return a Null value in all columns from the Classes table when it finds a student who is not yet registered for any classes. If you think about the concept of a difference between two sets (discussed in Chapter 7, Thinking in Sets), the rows with a Null value in the columns from the Classes table represent the difference between the set of all students and the set of students who have registered for a class.

Likewise, if you ask for all classes and any students who registered for classes, the rows with Null values in the columns from the Students table represent the difference between the set of all classes and the set of classes for which students have registered. As we promised, using an OUTER JOIN with a test for Null values is an alternate way to discover the difference between two sets. Unlike a true EXCEPT operation that matches on entire rows from the two sets, you can specify the match in a JOIN operation on just a few specific columns (usually the primary key and the foreign key).


TABLE OF CONTENTS
   Part 1: How to construct and use SQL OUTER JOINs optimally
   Part 2: How to use the LEFT vs. the RIGHT OUTER JOIN
   Part 3: Using the FULL OUTER JOIN in SQL
   Part 4: SQL OUTER JOIN sample uses
   Part 5: SQL OUTER JOIN sample statements for queries

Exchange 2007 Storage Systems This chapter excerpt from SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL (2nd Edition) by John L. Viescas and Michael J. Hernandez, is printed with permission from Addison-Wesley Professional, Copyright 2007.

Click here for the chapter download or purchase the book here.



Sound Off! -   Be the first to post a message to Sound Off!


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL/Transact SQL (T-SQL)
Create DDL table in SQL Server 2005 to audit DDL trigger activity
SQL Server source code analysis and management adds database security
SQL and SQL Server Tutorial and Reference Guide
Retrieve XML data values with XQuery in SQL Server 2005
How to use the LEFT vs. RIGHT OUTER JOIN in SQL
Using the FULL OUTER JOIN in SQL
SQL OUTER JOIN sample uses
SQL OUTER JOIN sample statements for queries
Stored procedure to monitor long-running jobs in SQL Server 2000
Five sqlcmd features to automate SQL Server database tasks
SQL/Transact SQL (T-SQL) Research

SQL Server performance and tuning
SQL Server PerfMon counters for access methods and buffer manager
Find size of SQL Server tables and other objects with stored procedure
Monitor SQL Server disk I/O with PerfMon counters
SQL Server tempdb best practices increase performance
SQL Server PerfMon counters for Windows operating system (OS)
How to maintain SQL Server indexes for query optimization
Performance tuning for SQL Server 2005 and Exchange running on SBS
Troubleshoot SQL Server 2005 temporary table performance problems
Maintain large SQL Server database and resolve website 'Timeout Error'
Use SQL Profiler to find long running stored procedures and commands

SQL Server database design and modeling
SQL Server tempdb best practices increase performance
FAQ: SQL Server databases how-to
How to maintain SQL Server indexes for query optimization
How to retrieve SQL Server database disk space in use
Maintain large SQL Server database and resolve website 'Timeout Error'
How to use the LEFT vs. RIGHT OUTER JOIN in SQL
Using the FULL OUTER JOIN in SQL
SQL OUTER JOIN sample statements for queries
SQL OUTER JOIN sample uses
Designing SQL Server non-clustered indexes for query optimization

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
ACID  (SearchSQLServer.com)
commit  (SearchSQLServer.com)
DAO  (SearchSQLServer.com)
fetch  (SearchSQLServer.com)
OLE DB  (SearchSQLServer.com)
query  (SearchSQLServer.com)
SQL  (SearchSQLServer.com)
T-SQL  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsWebcastsWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts