ImplementationINNER JOIN

Structured Query Language is used to allow database developers to generate queries and interrogate the data held in a database. SELECT, FROM, WHERE, AND and OR are key areas of SQL.

Part ofComputing ScienceDatabase design and development

INNER JOIN

Used to exemplify equi-join

An equi-join is one type of INNER JOIN that can be applied using SQL.

Equi-joins will be used with SELECT statements to select data from more than one table. If the conditions specified in the INNER JOIN are met, then data from both tables is usually returned.

The word equi is used in reference to the = operator.

INNER JOINS can support use of < and > operators but it is only necessary to understand use of the = operator at National 5 level.

We already have a Pupil table, with the updated version shown below:

PupilIDFirst NameSurnameClassPastoral Care TeacherMerit Points
0001NisaSingh1T1Mr Harley0
0002DavieBoland1I2Mrs Aitken0
0003AmyJones1M1Mr Gilchrist0
0004JamesMcNamee1T1Mr Harley0
0005PawelMcTavish1I1Mrs Aitken0
0006AntoniaGallagher1M2Mr Gilchrist0
0007AmyMcNamee1T1Mr Harley0
0008TabisoMatazinadze1T1Mr Harley0
0009BeatricePanner1T2Mr Harley0
0011FrankieAl-Robeye1M2Mr Gilchrist0
PupilID0001
First NameNisa
SurnameSingh
Class1T1
Pastoral Care TeacherMr Harley
Merit Points0
PupilID0002
First NameDavie
SurnameBoland
Class1I2
Pastoral Care TeacherMrs Aitken
Merit Points0
PupilID0003
First NameAmy
SurnameJones
Class1M1
Pastoral Care TeacherMr Gilchrist
Merit Points0
PupilID0004
First NameJames
SurnameMcNamee
Class1T1
Pastoral Care TeacherMr Harley
Merit Points0
PupilID0005
First NamePawel
SurnameMcTavish
Class1I1
Pastoral Care TeacherMrs Aitken
Merit Points0
PupilID0006
First NameAntonia
SurnameGallagher
Class1M2
Pastoral Care TeacherMr Gilchrist
Merit Points0
PupilID0007
First NameAmy
SurnameMcNamee
Class1T1
Pastoral Care TeacherMr Harley
Merit Points0
PupilID0008
First NameTabiso
SurnameMatazinadze
Class1T1
Pastoral Care TeacherMr Harley
Merit Points0
PupilID0009
First NameBeatrice
SurnamePanner
Class1T2
Pastoral Care TeacherMr Harley
Merit Points0
PupilID0011
First NameFrankie
SurnameAl-Robeye
Class1M2
Pastoral Care TeacherMr Gilchrist
Merit Points0

The database also has a table holding information on members of the school orchestra. This table is called 'Orchestra' and contains the following details:

OrchestraMemberIDPupilIDGroupTuition
010004WoodwindYes
020007StringsNo
030008BrassYes
040003BrassNo
050009PercussionNo
OrchestraMemberID01
PupilID0004
GroupWoodwind
TuitionYes
OrchestraMemberID02
PupilID0007
GroupStrings
TuitionNo
OrchestraMemberID03
PupilID0008
GroupBrass
TuitionYes
OrchestraMemberID04
PupilID0003
GroupBrass
TuitionNo
OrchestraMemberID05
PupilID0009
GroupPercussion
TuitionNo
SELECT Orchestra.Group, Pupil.First Name, Pupil.Surname
FROM Orchestra
INNER JOIN Pupil ON Orchestra.PupilID = Pupil.PupilID;

Would return:

GroupFirst NameSurname
WoodwindJamesMcNamee
StringsAmyMcNamee
BrassTabisoMatazinadze
BrassAmyJones
Percussion BeatricePanner
GroupWoodwind
First NameJames
SurnameMcNamee
GroupStrings
First NameAmy
SurnameMcNamee
GroupBrass
First NameTabiso
SurnameMatazinadze
GroupBrass
First NameAmy
SurnameJones
GroupPercussion
First NameBeatrice
SurnamePanner

Pupil ID is the primary key in the Pupil table. It appears as a foreign key in the Orchestra table. The equi-join is used to match the values held in both tables, so that results are only returned where a match exists.

Using a WHERE clause can further refine the results. In this example, only pupils who are part of the brass group are returned.

SELECT Orchestra.Group, Pupil.First Name, Pupil.Surname
FROM Orchestra
INNER JOIN Pupil ON Orchestra.PupilID = Pupil.PupilID 
WHERE Orchestra.Group = ‘Brass’;
GroupFirst NameSurname
BrassTabisoMatazinadze
BrassAmyJones
GroupBrass
First NameTabiso
SurnameMatazinadze
GroupBrass
First NameAmy
SurnameJones