Skip to content
SPM Tuition
Computer Science · Database development

Joining related tables

You can query one table, but asking for data from two tables gives strange results.

A join links rows from two tables where the foreign key matches the primary key. An INNER JOIN keeps only the rows that have a match in both tables.

This lesson follows normalising a small data set. Next comes aggregation and filtering.

How is a join written?

SELECT Student.Name, House.HouseName
FROM Student
INNER JOIN House
ON Student.HouseID = House.HouseID;

The ON line names the matching columns. Writing the table name before the column, such as Student.HouseID, shows which table each column comes from.

Worked example: students and houses

Original Student table:

StudentID Name HouseID
1 Aina 1
2 Bala 2
3 Chen 1
4 Dina 2

Original House table:

HouseID HouseName
1 Merah
2 Biru
3 Hijau

Predict the result. Each student row matches the house with the same HouseID. Aina and Chen match Merah, and Bala and Dina match Biru.

Name HouseName
Aina Merah
Bala Biru
Chen Merah
Dina Biru

There are four rows. Hijau does not appear, because no student has HouseID 3.

The mistake that costs marks

The common slip is to leave out the ON condition. The system then pairs every student with every house.

Query Rows returned
with ON Student.HouseID = House.HouseID 4
without a matching condition 4 × 3 = 12

The twelve rows show Aina with Merah, Biru and Hijau, which is wrong. The fix is to count: a correct join on a one-to-many link returns at most as many rows as the child table has.

Check yourself

The Student table above gains a fifth student, Emir, with HouseID 3. How many rows does the same INNER JOIN return, and what is Emir’s house?

Answer

It returns five rows. Emir has HouseID 3, which matches Hijau, so his row is Emir and Hijau. Hijau now appears in the result because a student belongs to it.

What to study next

After joining, you usually count or total the results. Continue with using aggregation and filtering, then slow down with SQL reasoning in a fictional database.

If you want a teacher to go through joins with you, see online one-to-one Computer Science tuition.

Common questions

What does a join do?

A join combines rows from two tables where a condition is true, usually where a foreign key equals a primary key. The result looks like one wider table. It lets you ask for a student's name and house name in a single query.

What does ON do in a join?

ON states which columns must match, such as Student.HouseID = House.HouseID. Without it, every row of one table pairs with every row of the other, which gives a large and meaningless result.

Which rows does INNER JOIN keep?

Only rows that have a match in both tables. A student with no matching house, or a house with no students, does not appear in the result.

If join results surprise you, one-to-one Computer Science lessons let a teacher ask you to predict the row count before you run each query.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.