By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,481 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

SQL JOINs

NeoPa
Expert Mod 15k+
P: 31,602
Introduction
Joins, in SQL, are a way of linking Recordsets together.
They involve restricting which data is returned in the output Recordset. When no join is specified but two Recordsets are, then a cartesian product is produced which specifies no restrictions. Conceptually, a JOIN is applied before any WHERE clause which may be specified.

NB. Full Outer Joins are not supported in Access (Jet) SQL.

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 Access (Jet) SQL, tables can be joined in various ways.
  • INNER JOIN
    In its simplest form, this can be :
    Expand|Select|Wrap|Line Numbers
    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 (Outer Joins)
    In its simplest form, this can be :
    Expand|Select|Wrap|Line Numbers
    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.
  • Full Outer Join (Not supported in Access (Jet) SQL)
    In its simplest form, this can be :
    Expand|Select|Wrap|Line Numbers
    1. FROM Table1 OUTER 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, EITHER the first OR second matching record doesn't exist. This sets both of the input Recordsets as the same priority as the other. It includes all records of both Recordsets. 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.
  • No Join (Cartesian Product)
    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 unjoined links are 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 (Cartesian Product).
    See the examples below to get a better understanding of this.


Examples

Expand|Select|Wrap|Line Numbers
  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
Expand|Select|Wrap|Line Numbers
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 INNER JOIN Table2
  3.   ON Table1.Name1=table2.Name2
Output Recordset
Expand|Select|Wrap|Line Numbers
  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 OUTER JOIN Example
Expand|Select|Wrap|Line Numbers
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 LEFT JOIN Table2
  3.   ON Table1.Name1=table2.Name2
Output Recordset
Expand|Select|Wrap|Line Numbers
  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 OUTER JOIN Example
Expand|Select|Wrap|Line Numbers
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 RIGHT JOIN Table2
  3.   ON Table1.Name1=table2.Name2
Output Recordset
Expand|Select|Wrap|Line Numbers
  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.

FULL OUTER JOIN Example
Expand|Select|Wrap|Line Numbers
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1 OUTER JOIN Table2
  3.   ON Table1.Name1=table2.Name2
Output Recordset
Expand|Select|Wrap|Line Numbers
  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. Null     Null      Charlie  301
  9. Null     Null      Charlie  302
  10. Don      41        Don      401
All records are included, some more than once if there are multiple matches in both input Recordsets (EG. Andy). Bob and Charlie are both included, but the missing data is represented by Nulls. There is no data that is not represented anywhere.
NB. This type of Join is not supported in Access (Jet) SQL.

No Join (Cartesian Product) Example
Expand|Select|Wrap|Line Numbers
  1. SELECT Name1,Value1,Name2,Value2
  2. FROM Table1,Table2
Output Recordset
Expand|Select|Wrap|Line Numbers
  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.
Feb 26 '07 #1
Share this Article
Share on Google+