473,808 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL JOINs

NeoPa
32,579 Recognized Expert Moderator MVP
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 18516

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

Similar topics

6
2088
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. Something isn't correct with my logic so could anyone give me some pointers on it? I have 2 tables: CourseRoster: ID CourseID StudentID StudentType
3
6421
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
4
3999
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 the columns is there any specific order i need to follow.
1
4663
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 sequential joins. eg. if i have 4 tables A, B, C, D and the join conditions are A Inner join B, B Inner Join C, C Left Outer join D then i am constructing joins as :
4
4112
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 results, then doing a self left join with those results, etc. What puzzles me is that the time required for the query seems to grow exponentially as I add additional left joins, which I didn't expect. I expected the inner select to return about 25...
4
2048
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 ? thanks
7
31570
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" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception "The column prefix 'tempdb' does not match with a table name or alias name used in the query.")
2
19725
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 some of these joins are inner joins and some are Left outer joins. table1 inner joined with table 2 table2 inner join with table3 table2 inner join with table4 table2 left join with table5
9
11923
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: http://www.codinghorror.com/blog/archives/000976.html
36
2503
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 joins vanish from at least one (seemingly random) query. I've always regarded the Vanishing Joins bug as a symptom of corruption. When it happens, I usually give my users advice on how to recover from corruption, and how to avoid it in the future....
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.