A Gentle Introduction to


Pick an engine:

Movie Database: The JOIN

The obvious questions about this database involve all three tables. Who appear in such-and-such a film, or which films has so-and-so appeared in. In fact we can answer some of these questions using nested SELECT statements that have already been covered.

A join allows us to link two tables which have a "common" field. The id field of the actor table is referred to by the actorid field of the casting table. A join of these tables on these fields produces a table which includes all of the attributes of both tables.

Joining two tables

1 Join casting and actor on actorid/id

The result of the above gives one row for every element of the casting table which relates to John Hurt. In addition to the actorid we have the name of the actor involved.

Joining three tables

The it makes sense to join the above table with the movie table. The obvious field to join on is the movieid field.

2 Joining movies, casting and actors.

The result now again has one row for every element of the casting table, this time we get details of the movies as well as the name of the actor.

Notice that in some cases we refer to a field using just the field name (e.g. actorid) and sometimes we preceed the field name with the table name (e.g. casting.actorid). You must include the table name if the field names are not unique.