advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Blue Star Average Rating: 4.6/5 | Rate this item | 51 users have rated this item.
 Print Print
 
Getting the Right Data with SQL Joins
Associating tables in SQL is usually a breeze. But what happens when you find yourself looking at the completely wrong set of records and you don't know why? Find out how correctly applying joins can help you understand and avoid these mysterious data returns.  

 
advertisement
he cornerstone of the relational database system is its multiple tables and the ability to associate those tables with one another so you can combine data from different but related tables. Most of the time these associations are intuitive and work just as you expect. However, problems can arise when one of these associations returns unexpected results. In other words, the records you get, aren't the records you know you should be seeing.

Fortunately, you can dynamically relate different tables by applying what's known as a join. Technically, a join is the operation used when selecting data to create these relationships at retrieval time. What that means to you is that a join determines which records are selected and which aren't. In this article, we'll introduce you to several types of joins supported by SQL and show you how to correctly apply them to get the data you need.

SQL Standards
Technically, a join is an operation (it may be easier to think of this operation as a rule) that defines a relationship to determine which records are retrieved or acted upon. What that means is that a join is conditional—similar to a WHERE clause or criteria expression—in that the join specifies which records (rows) are selected in both tables. Certainly, the type of join significantly impacts which records are retrieved or acted upon.

Before we get started, we should clarify that this article is about SQL joins, not about the way in which particular systems support SQL joins. Although, we will discuss actual examples, for syntax purposes, we won't delve into the many nuances of each version-specific SQL. Examples will include the most basic syntax possible for simplicity's sake. For instance, we won't show alias syntax because they're all different—those details are better left to articles that deals with version-specific SQL information.

For the most part, a join can be specified in a FROM or WHERE clause, so be careful where you specify them. Because the data engine executes the clauses in a specific sequence, placement can affect the results. Use the following guidelines to determine the best place for a particular join, although most of the time, the position won't make any difference:

  • Generally, the data engine fulfills the FROM clause before applying any other clauses. Adding the join here eliminates records before they're evaluated by any other clauses. As a rule, you'll find this is the recommended method.
  • Any records that make it past the FROM clause arethen evaluated by an implicit join condition specified in the WHERE clause (if any).
To specify a join in the FROM clause, use the form:

SELECT fldlist | *
FROM table1 join table2 
  [ON (condition)]
Use it where table1 and table2 represent the related tables, join is the type of join applied to the relationship, and condition specifies the columns you're comparing to restrict the query's results. Usually that condition is a simple expression that compares the primary and foreign key values in the form:

ON table1.primarykey = table2.foreignkey
At this point, its important to stress that the primarykey and foreignkey references can actually refer to any column, as long as the data types match, because you can create a temporary relationship between two non-key fields (although you probably won't do so very often). This article uses the primarykey/foreignkey terms because they help emphasis the action that is going on behind the scenes. It is much more difficult to visualize the result of joining two non-key columns. However, technically, you aren't restricted to the primary/foreign key fields, and you may need the capability some day. Just remember that a relationship based on non-key columns is temporary and exists only between the two tables involved.

  Next Page: A Few Guidelines
Page 1: IntroductionPage 4: The Outer Joins
Page 2: A Few GuidelinesPage 5: The Outer Joins (cont'd)
Page 3: Joining Three or More TablesPage 6: The Cross
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES