SQL JOINs

Visited 799 times | Submited on 2007-06-05 09:43:06

Introduction
Joins, in SQL, are a way of linking Recordsets together.
Usually, they involve restricting which data is returned in the output Recordset, although this is not true for Outer Joins (discussed later). Conceptually, a JOIN is applied before any WHERE clause which may be specified.
When Recordsets are JOINed they typically produce more records in the output Recordset than there are in the input Recordsets. This is not always true though.
JOINed Recordsets convert two input Recordsets into a single output Recordset which contains the fields of both of the input Recordsets.
In SQL, tables can be joined in various ways.

  • INNER JOIN
    In its simplest form, this can be :
    1. FROM Table1 INNER JOIN Table2 ON Table1.Field=Table2.Field

    This produces a record in the output Recordset for each combination of every record in one Recordset with every record in the other Recordset... where the ON statement is TRUE. If there is no matching record then it will not be included in the output Recordset. If there is more than one then all combinations will be included in the output Recordset.
    See the examples below to get a better understanding of this.
  • LEFT JOIN; RIGHT JOIN
    In its simplest form, this can be :
    1. FROM Table1 LEFT JOIN Table2 ON Table1.Field=Table2.Field

    This produces a record in the output Recordset for each combination of every record in one Recordset with every record in the other Recordset... where the ON statement is TRUE... OR the second (for a LEFT JOIN) or first (for a RIGHT JOIN) record doesn't exist. This sets one of the input Recordsets as a higher priority than the other. It includes all records of one Recordset but only those of the other Recordset that are matched. If there is more than one matching record then all combinations will still be included in the output Recordset.
    See the examples below to get a better understanding of this.
  • Outer Join
    Notice this does not conform to the format of the others.
    In SQL, this is specified by simply listing the two tables separated by a comma (,).
    The way an Outer join is processed is that every combination of all the records in both input Recordsets is returned as a record in the output Recordset. This can produce a large number of records from relatively small input Recordsets.
    See the examples below to get a better understanding of this.

Examples

  1. Table1           Table2
  2. Name1    Value1    Name2    Value2
  3. Andy     11        Andy     101
  4. Andy     12        Andy     102
  5. Bob      21        Charlie  301
  6. Bob      22        Charlie  302
  7. Don      41        Don      401

INNER JOIN Example
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 INNER JOIN Table2
  3.   ON Table1.Name1=table2.Name2


Output Recordset

  1. Name1    Value1    Name2    Value2
  2. Andy     11        Andy     101
  3. Andy     11        Andy     102
  4. Andy     12        Andy     101
  5. Andy     12        Andy     102
  6. Don      41        Don      401
Neither Bob nor Charlie appear at all using this JOIN type as neither is included in both tables.

LEFT JOIN Example
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 LEFT JOIN Table2
  3.   ON Table1.Name1=table2.Name2

Output Recordset

  1. Name1    Value1    Name2    Value2
  2. Andy     11        Andy     101
  3. Andy     11        Andy     102
  4. Andy     12        Andy     101
  5. Andy     12        Andy     102
  6. Bob      21        Null     Null
  7. Bob      22        Null     Null
  8. Don      41        Don      401
Bob is included but, with no matching records from Table2, the fields which come from Table2 are left as Null. Charlie doesn't appear at all as it doesn't appear in Table1.

RIGHT JOIN Example
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 RIGHT JOIN Table2
  3.   ON Table1.Name1=table2.Name2

Output Recordset

  1. Name1    Value1    Name2    Value2
  2. Andy     11        Andy     101
  3. Andy     11        Andy     102
  4. Andy     12        Andy     101
  5. Andy     12        Andy     102
  6. Null     Null      Charlie  301
  7. Null     Null      Charlie  302
  8. Don      41        Don      401
Charlie is included but, with no matching records from Table1, the fields which come from Table1 are left as Null. Bob doesn't appear at all as it doesn't appear in Table2.

Outer Join Example
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1,Table2

Output Recordset

  1. Name1    Value1    Name2    Value2
  2. Andy     11        Andy     101
  3. Andy     11        Andy     102
  4. Andy     11        Charlie  301
  5. Andy     11        Charlie  302
  6. Andy     11        Don      401
  7. Andy     12        Andy     101
  8. Andy     12        Andy     102
  9. Andy     12        Charlie  301
  10. Andy     12        Charlie  302
  11. Andy     12        Don      401
  12. Bob      21        Andy     101
  13. Bob      21        Andy     102
  14. Bob      21        Charlie  301
  15. Bob      21        Charlie  302
  16. Bob      21        Don      401
  17. Bob      22        Andy     101
  18. Bob      22        Andy     102
  19. Bob      22        Charlie  301
  20. Bob      22        Charlie  302
  21. Bob      22        Don      401
  22. Don      41        Andy     101
  23. Don      41        Andy     102
  24. Don      41        Charlie  301
  25. Don      41        Charlie  302
  26. Don      41        Don      401

Every possible combination is included.

Source: thescripts.com



Add your comment

Name:(required)
E-mail address:(optional)
Comment:(required)
Repeat the number for validation: (required)

Browse by Tags:


Related Articles:

Text Link Ads

Statistics

Total 296 articles submitted
Latest submission at January 28, 2008 15:13

Feedback

Use this email below to send us your suggestions and feedback. We value your opinion.
info (at) theitarticles.com