The Result Data Newsletter   
Volume 711 - November 2007   
© Copyright 2007 Result Data Consulting, Ltd.  614-505-0770  www.resultdata.com   

    Result Data Home Page  |  Newsletter Archive  |  Upcoming Events  |  Classes & Workshops  |  Request Information
Upcoming Events:  Microsoft Data Mgt & BI Seminar: 12/14, Microsoft SQL Server User Group: 12/14

Return to Newsletter Contents...

 

SQL  - There's no difference between a Joins and a Where clause - 'Joins don't buy anything'

by: Charles Tournear, Sr. Consultant, MCT, MCSE, MCSD, MCDBA, CRCP

The SQL language has been an ANSI standard for requesting data from relational database systems since 1986.  But the standards have changed since there original definition.  In the ANSI standard 1986, 1988, and 1992, joins were defined with a table list and connected by using a where clause.  Starting with ANSI standard 1999 and higher the syntax has changed to using a JOIN and ON clause.

Examples of the ANSI standard 1992:

Inner Join (only retrieve rows that both tables have one or more fields in common)

      SELECT field1, field2

      FROM table1, table2

      WHERE table1.PKfield = table2.FKfield

            AND field1 = ‘value’

Left Outer Join  (retrieve all rows from table on the left of the = sign and only matching rows from the other table)

            SELECT field1, field2

      FROM table1, table2

      WHERE table1.PKfield *= table2.FKfield

            AND field1 = ‘value’

Right Outer Join (retrieve all rows from table on the right of the = sign and only matching rows from the other table)

      SELECT field1, field2

      FROM table1, table2

      WHERE table1.PKfield =* table2.FKfield

            AND field1 = ‘value’

 

Examples of the same queries using ANSI standard 1999 syntax:

Inner Join (only retrieve rows that both tables have one or more fields in common)

           

            SELECT field1, field2

      FROM table1 INNER JOIN table2

      ON table1.PKfield = table2.FKfield

      WHERE field1 = ‘value’

Left Outer Join  (retrieve all rows from table on the left of the = sign and only matching rows from the other table)

            SELECT field1, field2

      FROM table1 LEFT OUTER JOIN table2

      ON table1.PKfield = table2.FKfield

      WHERE field1 = ‘value’

Right Outer Join (retrieve all rows from table on the right of the = sign and only matching rows from the other table)

            SELECT field1, field2

      FROM table1 RIGHT OUTER JOIN table2

      ON table1.PKfield = table2.FKfield

      WHERE field1 = ‘value’

ANSI standard 1999 also added some additional join types that weren’t available using 1992 WHERE syntax such as NATURAL JOIN and FULL JOIN.  Natural join automatically join two tables on all common field names.  Full join is a combination of a left outer and a right outer join to pull data from both tables whether or not there are matching records or not.

Another change in the ANSI standard is how NULL‘s are handled.  In ANSI standard 92, the NULL key word was used to indicate blank in every occurrence of its use.  Beginning with ANSI standard 1999 the keyword NULL function depends on how it is used.  “= NULL” is now considered an invalid statement.  To check for blanks the new syntax is “IS NULL”.  Depending on how well a system follows the rules for ANSI standards, this could really mess up the results if you use WHERE to define the link between two tables instead of using JOIN syntax.

For example consider the 92 syntax:

            SELECT field1, field2

      FROM table1, table2

      WHERE table1.PKfield = table2.FKfield

            AND field1 = ‘value’

In the 1999 syntax if it is possible that PKfield or FKfield could be blank or NULL, then the result of the = comparison would be invalid and cause unexpected output.  Conditional statements such as IF, CASE, or WHERE treat any comparison to a value that could return NULL as invalid.  Where as the ON clause of a JOIN always treats the NULL value as meaning blank.

To give an example, consider the following code:

      DECLARE @VAR1 int

      DECLARE @VAR2 int

      SET @VAR1 = 1

      SET @VAR2 = NULL

     

      IF @VAR1 = @VAR2

            SELECT ‘true’

      ELSE

            SELECT ‘false’

 

      IF @VAR1 <> @VAR2

            SELECT ‘true’

      ELSE

            SELECT ‘false’

In the above example the first IF clause would return false, which we would expect since 1 does not equal NULL.  The second IF clause would also return false, which we would not expect since it is true that 1 does not equal NULL.  In both cases, the comparison of any value to NULL returns Invalid which in this case is interpreted as a false result.  No error will occur.

Another issue with using the WHERE clause for Joins, was that the where clause could become very confusing combining links and row filters in the same clause.  Which parts are actually linking tables and which are used to filter which rows to return? 

The statement then that there is no difference between using the WHERE clause versus JOIN syntax, while it was true for ANSI standard 1992, it is definitely NOT true for any system that supports ANSI standard 1999 or higher.  In general then to be compatible with ANSI and most systems that support the current version of the SQL standard, you should use the JOIN syntax to link tables.  Each part of a query then has a specific task.  SELECT is to choose columns of data for output; FROM is to determine how tables should be linked; and WHERE is to select which rows from each table are to be returned or used in calculations.

Go to Top  |  Return to Newsletter Contents

 

The Result Data Newsletter is published approximately once a month to share the latest information on business intelligence, data management and CRM. There should be a link below to allow you to change or remove yourself from our list. We take your requests very seriously. If you have any difficulty please contact us at 614-505-0770 and we will make sure that your request is handled properly. This is not intended to be an unsolicited message and you can reach us in person if needed.

© Copyright 2007 Result Data Consulting, Ltd. - All Rights Reserved
All trademarks and copyrights are the property of their respective owners. This information is provided without warranty.
Announcements
Fall Training Special
Get a jump on your Christmas shopping!  Schedule and attend any public training class now through December and receive a FREE $50 gift card to Target, Barnes & Nobles or Best Buy (restrictions apply).  Call 614-505-0770 for further details and restrictions.
Next Microsoft SQL Server User Group
The next Microsoft User Group is on Dec. 14, 2007.  Call 614-505-0770 or click here to reserve your seat.
Next Microsoft Data Management & BI Seminar
The next free Microsoft BI seminar is on Dec. 14, 2007.  Call 614-505-0770 or click here to reserve your seat.
Looking for a Few Good Men and Women
Join our award winning team of Business Intelligence consultants and .Net Software developers.
Send your resume and salary requirements to:
jobs@resultdata.com