SQL Joins & Reading Code (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

SQL joins and reading code

What is an equi-join?

  • An equi-join is used to combine data from two linked tables in a relational database

  • It retrieves data where the primary key (PK) in one table matches the foreign key (FK) in another

  • This ensures that only related records are displayed

Structure of an equi-join

  • When querying two linked tables, you must include the join condition in the WHERE clause

SELECT field1, field2

FROM table1, table2

WHERE table1.primaryKey = table2.foreignKey;

  • The condition links the two tables correctly so that the output shows matched data from both

Example

Table: Employees

EmployID

Name

Salary

City

1

Fynn Roberts

45000

London

2

Zarmeen Azra

52000

Manchester

3

Ella Stanley

39500

Birmingham

Table: Departments

DepartID

EmployID

Department

Manager

Email

1

1

HR

Sally Jones

[email protected] (opens in a new tab)(opens in a new tab)

2

2

Sales

Peter Evans

[email protected] (opens in a new tab)(opens in a new tab)

3

3

Marketing

Stuart Davies

[email protected] (opens in a new tab)(opens in a new tab)

  • Select the name, manager and email address of employees in the sales department

SELECT Employees.Name, Departments.Manager, Departments.Email

FROM Employees, Departments

WHERE Employees.EmployID = Departments.EmployID

AND Department = 'Sales';

  • FROM Employees, Departments means data is taken from both tables

  • WHERE Employees.EmployID = Departments.EmployID is the equi-join condition linking the tables

  • AND Department = 'Sales' filters the results to show only sales department employees

Output:

A table with columns for Name, Manager, and Email. The data row contains "Zarmeen Azra," "Peter Evans," and "sales@myemail.com."

Understanding and debugging SQL joins

  • A missing join condition is the most common error

  • If the equi-join condition is missing:

SELECT Employees.Name, Departments.Manager

FROM Employees, Departments;

  • The database tries to match every employee with every department, producing incorrect results

  • You must include the join condition to correctly link both tables

Reading and explaining SQL code

  • When reading SQL that includes joins, check:

    1. Tables: Are both tables named correctly?

    2. Fields: Do the field names exist in those tables?

    3. Join condition: Is there a line linking the PK and FK?

    4. Filtering: Are WHERE, AND, and OR used correctly to filter results?

SELECT Player.forename, Team.town

FROM Player, Team

WHERE Player.teamName = Team.teamName

AND town = 'Paisley';

  • Retrieves the forename and town of all players whose team is based in Paisley

  • The join Player.teamName = Team.teamName ensures each player is correctly matched to their team

Examiner Tips and Tricks

When asked to retrieve data from two linked tables:

  • Always include the equi-join condition (table1.PK = table2.FK)

  • Always check for correct field names and logical filters

  • If the question asks you to “explain why a query doesn’t work,” look for a missing join, wrong field name, or incorrect condition

Summary

SQL feature

Purpose

Example

Equi-join

Combine data from two related tables

WHERE Employees.EmployID = Departments.EmployID

Filtering

Narrow results

AND Department = 'Sales'

Common error

Missing join condition (produces incorrect output)

FROM Employees, Departments without WHERE condition

Worked Example

Scenario: The Botanical Database

A database is used by an environmental group to track edible plants and the climates they grow in. The database consists of two linked tables: Plant and Climate.

  • The Climate table contains details like climateType and temperatureRange.

  • The Plant table contains details like plantName, ediblePart, and soilType.

  • The link between the tables is made using the primary key climateRef (in Climate) and the foreign key climateRef (in Plant).

The group wishes to generate a detailed list of all edible plants that produce fruit and grow in tropical climates. The list should be ordered to display the plants that grow in the highest temperatureRange first

(i) Write the full SQL statement that implements the design above, including the necessary equi-join

[3]

An intern wrote the following SQL statement in an attempt to find plants that grow in Loam soil and a Mediterranean climate.

SELECT climateRef, temperatureRange, plantName, ediblePart
FROM Climate
WHERE soilType = "Loam"
AND climateType = "Mediterranean";

(ii) State two specific reasons why this SQL statement will not produce the expected output, based on the required structure of the two linked tables

[2]

Answers

(i)

SELECT plantName, climateType, temperatureRange FROM Plant, Climate [1 mark]

WHERE Plant.climateRef = Climate.climateRef [1 mark]

AND ediblePart = 'Fruit' AND climateType = 'Tropical' ORDER BY temperatureRange DESC; [1 mark]

(ii)

  • missing the equi-join condition in the WHERE clause [1 mark]

    • This is required because data is being selected from two linked tables (Plant and Climate)

  • FROM clause only specifies the Climate table [1 mark]

    • But the query attempts to search using the field soilType, which is an attribute located in the Plant table (OR: The Plant table is missing from the FROM clause)

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.