473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

SQL JOINs

NeoPa
32,556 Expert Mod 16PB
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
0 18469

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jgalzic | last post by:
Hi, I'm having trouble doing joins correctly on two tables. I've read up a lot about the different types of joins and tried lots of variations on inner, outer, and left joins with no avail....
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: Sri | last post by:
I am writing a download process in which i have a condition where i need to join four tables. Each table have lot of data say around 300000 recs. my question is when i am doing the joins on...
1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.