Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Doctors
Patients
ID FullName MedCntrID
ID FullName DocID
1 Joe Manners 1
1 Jim Thick 4
2 Sue Tongs 1
2 Tom Small 2
3 Jeff Spine 1
3 Al Downs 4
4 Mary Rasch 2
4 Ann Hills 1
5 Tom Thumb 2
5 Tim Burrow 3
6 Norm Lobe 3
6 Jane Fern 5
7 Sam Broom 2
8 Gary Far 1
9 Bill Out 5
10 Dave Bell 4
11 Fred Overs 5
12 Greg Double 1
13 Bob Marks 9
FROM
Left_Table
JOIN_TYPE
Right_Table
ON
Join_Condition
SELECT D.FullName AS DoctorName, P.FullName AS PatientName
FROM
Doctors D
INNER JOIN
Patients P
ON
D.ID = P.DocID
Doctors D
1
Joe Manners 1
Patients P
D.ID = P.DocID
4 Ann Hills
1
8 Gary Far
1
12 Greg Double
1
DoctorName
PatientName
Joe Manners
Ann Hills
Joe Manners
Gary Far
Joe Manners
Greg Double
Doctors D
2 Sue Tongs 1
Patients P
D.ID = P.DocID
DoctorName
PatientName
Joe Manners
Ann Hills
Joe Manners
Gary Far
Joe Manners
Greg Double
Sue Tongs
Tom Small
Sue Tongs
Sam Broom
Jeff Spine
Tim Burrow
Mary Rasch
Jim Thick
Mary Rasch
Al Downs
Mary Rasch
Dave Bell
Tom Thumb
Jane Fern
Tom Thumb
Bill Out
Tom Thumb
Fred Overs
Norm Lobe
Bob Marks
Left_Table
Join_Condition
Right_Table
Right_Table
Right_Table
Left_Table
Right_Table
SELECT D.FullName AS DoctorName, P.FullName AS PatientName
FROM
Doctors D
LEFT OUTER JOIN
Patients P
ON
D.ID = P.DocID
DoctorName
PatientName
Joe Manners
Ann Hills
Joe Manners
Gary Far
Joe Manners
Greg Double
Sue Tongs
Tom Small
Sue Tongs
Sam Broom
Jeff Spine
Tim Burrow
Mary Rasch
Jim Thick
Mary Rasch
Al Downs
Mary Rasch
Dave Bell
Tom Thumb
Jane Fern
Tom Thumb
Bill Out
Tom Thumb
Fred Overs
Norm Lobe
NULL
Left_Table
Right_Table
Left_Table
SELECT D.FullName AS DoctorName, P.FullName AS PatientName
FROM
Doctors D
RIGHT OUTER JOIN
Patients P
ON
D.ID = P.DocID
DoctorName
PatientName
Joe Manners
Ann Hills
Joe Manners
Gary Far
Joe Manners
Greg Double
Sue Tongs
Tom Small
Sue Tongs
Sam Broom
Jeff Spine
Tim Burrow
Mary Rasch
Jim Thick
Mary Rasch
Al Downs
Mary Rasch
Dave Bell
Tom Thumb
Jane Fern
Tom Thumb
Bill Out
Tom Thumb
Fred Overs
NULL
Bob Marks
Left_Table
Right_Table
SELECT D.FullName AS DoctorName, P.FullName AS PatientName
FROM
Doctors D
FULL OUTER JOIN
Patients P
ON
D.ID = P.DocID
Norm Lobe
NULL
DoctorName
PatientName
Joe Manners
Ann Hills
Joe Manners
Gary Far
Joe Manners
Greg Double
Sue Tongs
Tom Small
Sue Tongs
Sam Broom
Jeff Spine
Tim Burrow
Mary Rasch
Jim Thick
Mary Rasch
Al Downs
Mary Rasch
Dave Bell
Tom Thumb
Jane Fern
Tom Thumb
Bill Out
Tom Thumb
Fred Overs
NULL
Bob Marks
Left_Table
Right_Table
SELECT D.FullName AS DoctorName, P.FullName AS PatientName
FROM
Doctors D
CROSS JOIN
Patients P
Joe Manners
Jim Thick
Joe Manners
Tom Small
Joe Manners
Al Downs
Joe Manners
Ann Hills
Joe Manners
Tim Burrow
Norm Lobe
Fred Overs
Norm Lobe
Norm Lobe
Left_Table
SELECT ....
FROM Patients P
INNER JOIN Doctors D [i]*NOTE 1[/i]
ON P.DocID = D.ID
LEFT OUTER JOIN MedicalCenters MC [i]*NOTE 2[/i]
ON D.MedCntrID = MC.ID
[code]
[i]*NOTE 1[/i] INNER JOIN because you only want patients that have a doctor that is on your register
[i]*NOTE 1[/i] Left OUTER JOIN because you want the information about the medical center if you have it but if you don't you still want the record to be included. If you made this a INNER JOIN then if a Doctor's medical center was not on record then the doctor would not be included in the result set meaing that any of their patients would not be on the result set since Patients and Doctors is done with an INNER JOIN too.
3) You can have multiple conditions for the ON clause just like you can in a WHERE clause. Make sure you know the difference between a JOIN condition and a WHERE clause. Though they look similiar in task they can execute slightly different giving unexpected results if you are not careful.
4) Use Alias in tables names they can make reading your queries much easiers
5) Format your queries for easy reading. A query doesn't run any faster if its all on 1 line or spread over 40 lines. As you can see from my formating style it is very easy to see what joins to what and the conditions they use which means less of a chance you made a error and will get unexpected results.
6) JOIN conditions are prime canidates for indexs. On small tables it doesn't matter but large tables will show great performance benifits from haveing the columns indexed.
I hope this little FAQ helps you understand JOINs better.
[i]Thanks to SQLSister and r937 for going over it and finding my mistakes and to r937 for some different, very useful, examples of cross joins. [/i]