A-Level Computer Science / Unit 8: Relational Databases and SQL

8.2.7 SQL DML: INNER JOIN and Data Maintenance

🔒 Lesson slides are available to signed-in users. Sign in

8.2.7 SQL DML: INNER JOIN and Data Maintenance

INNER JOIN combines fields from rows in two tables when the ON condition matches. INSERT adds rows, UPDATE changes them and DELETE removes them.

By the end of this page, you should be able to:

  • define and use the key terms accurately;
  • explain how the idea works using a new example;
  • apply the idea to a database design or SQL task.

1. Why join tables?

A relational design keeps different facts in different tables. When a report needs a member name and a booking time, a query can combine the related Member and Booking rows. INNER JOIN returns a combined row only when its join condition finds a match in both tables.

2. Use ON to match the keys

The ON condition says how the tables correspond. For example, Booking.MemberID = Member.MemberID matches each booking’s foreign key to the member’s primary key. Qualify field names when both tables contain a column with the same name. The syllabus limits these queries to at most two tables.

3. Maintain records with DML

INSERT adds a new record, UPDATE changes selected records, and DELETE removes selected records. List fields and values in the same order in an INSERT. Use WHERE to target the intended rows in UPDATE and DELETE.

4. Check the effect before changing data

An UPDATE or DELETE statement without an appropriate WHERE condition may affect every row. A foreign-key constraint may also prevent removing a record that other rows still reference. Check the target condition and the relationship rules before running a maintenance command.

Worked example

To show member names with booking start times, join Booking to Member using their shared MemberID. A booking whose MemberID has no matching Member row will not appear in an INNER JOIN result; a valid foreign-key constraint should normally prevent such a mismatch.

Common mistake to avoid

The join condition belongs after ON. It is not enough to name two tables; the query must state which fields identify corresponding records.

Interactive check

Choose an answer, read the explanation, and continue to the next example.

Try it

Check your understanding

Question 1 of 3
Score: 0 / 3

Independent practice

Try this original question before opening the suggested response.

Write a query showing Parcel.TrackingCode and Depot.DepotName using Parcel(TrackingCode, DepotID) and Depot(DepotID, DepotName). Then state one risk of omitting WHERE from UPDATE. (5)

Suggested response
SELECT Parcel.TrackingCode, Depot.DepotName
FROM Parcel INNER JOIN Depot
ON Parcel.DepotID = Depot.DepotID;

An UPDATE without a suitable WHERE condition may change every row in the table.

Answering clearly

Use the technical term, state what it does, and connect your explanation to the details in the question.