« The year 2025 - Birth… | Home | MBS FileMaker Advent … »

MBS FileMaker Advent calendar - Door 6 - Joins

Monkeybread Monkey as an elf
christmas tree Door 6
Joins
christmas tree

Today, I would like to introduce you to an important topic in SQL: Joins.

In FileMaker, we have relationships between the tables we work on, allowing us to relate data records to each other. We do not have these direct, fixed connections between two tables in SQL. Here, we have to create the relationship in the query, and we do that with Joins. There are different types of Joins, which we will now learn about using small examples so that we can use them later in your actual database. To do this, we will use a small section of our Movies table, which contains 5 movies for the example. We will simplify the primary key as a sequential number, as this is of course different in real life.

PrimarykeyName DirectorLaunchGenerated_Sale
1Die Hard John McTiernan1988141,603,197
2Christmas Vacation Jeremiah S. Chechik198971,319,526
3Love Actually Richard Curtis2003250,200,000
4The Polar Express Robert Zemeckis2004315,249,768
5Crown for Christmas Alex Zamm2015-

We also have a table called Roles2, which contains the roles in the films.

NameActor Movie
John McClaneBruce Willis 1
Hans GruberAlan Rickman 1
KarlAlexander Godunov 1
Clark GriswoldChevy Chase 2
Ellen GriswoldBeverly D'Angelo 2
Cousin Eddie Randy Quaid 2
DavidHugh Grant 3
KarenEmma Thompson 3
HarryAlan Rickman 3
Allie EvansDanica McKellar 5
King MaximillianRupert Penry-Jones 5
Princess TheodoraEllie Botterill 5

(INNER) JOIN

First, we have the INNER JOIN, which is the join I use the most. The INNER JOIN connects two tables so that only records that match in both tables are displayed. It therefore only shows common hits – anything that has no connection is hidden. In our case, this means that only movies and actors with matching primary keys and names will be displayed. If we apply an INNER JOIN, often referred to as just JOIN, to these tables with primary key = Movie, it looks like this:

SELECT Rols2.Movie, Movies2.Name, Movies2.Director, Movies2.Launch, Rols2.Actor 
FROM Movies2 
JOIN Rols2 
ON Movies2.PrimaryKey = Rols2.Movie

First, we have our SELECT part again, in which we specify the fields. This time, we want fields from both tables, which is why we also put the table name in front of it, separated by a dot. We specify all the fields from which we want the data. Then we come to the FROM part. Here, we first specify table A, then the word JOIN, and then table B. After we have specified the tables, we then add the keyword ON. After this, we can specify the connection over which the result will be created. In our example, we now want to match the two tables using the Primary Key and Movie fields if the values in these fields are the same.

FM.SQL.Execute" ; Get(FileName); "SELECT Rols2.Movie, Movies2.Name, Movies2.Director, Movies2.Launch, Rols2.Actor FROM Movies2 JOIN Rols2 ON Movies2.PrimaryKey = Rols2.Movie" ) ] Set Variable [ $r ; Value: MBS("FM.SQL.Text"; $SQLRes; ""; ""; ""; ""; "¶"; "-") ] Show Custom Dialog [ "Result" ; $r ] Set Variable [ $r ; Value: MBS("FM.SQL.ReleaseAll") ]

However, we not only have INNER JOIN, but also other types of JOIN.

LEFT JOIN

Let's move on to LEFT JOIN. A LEFT JOIN is a special form of joining two tables that guarantees that all records from the left table appear completely in the result, regardless of whether there are matching entries in the right table. You can think of it this way: The left table forms the fixed point, and for each of its rows, the database tries to find the corresponding information from the right table. If a matching record exists in the right table, both are combined and output. However, if there is no corresponding entry, the query still includes the row from the left table in the result. In this case, the fields in the right table are simply filled with NULL.


Nice to know:
In this context, NULL means that a specific field does not contain a value - not "0", not "empty", not "unknown", but truly no existing data entry.

LEFT JOINs are useful when the left table is the focal point of the evaluation. A classic example would be a list of all available products (left table) that needs to be linked to the sales for the current day (right table). Even if a product has not been sold today, it still appears in the result because it is in the left table. In this way, missing information, anomalies, or gaps can be clearly identified. In our example, we now want the movies in the left table and the roles in the right table. These are also linked via the primary key of Movies2 to the Movies field in the Rols2 table.


Set Variable [ $SQLRes ; Value: MBS("FM.SQL.Execute" ; Get(FileName); 
   "SELECT Movies2.Name, Movies2.Director, Movies2.Launch, Movies2.PrimaryKey, 
   Rols2.Movie, Rols2.Name, Rols2.Actor 
   FROM Movies2 LEFT JOIN Rols2 ON Movies2.PrimaryKey = Rols2.Movie" ) ]
Set Variable [ $r ; Value: MBS("FM.SQL.Text"; $SQLRes; ""; ""; ""; ""; "¶"; "-") ]
Show Custom Dialog [ "Result" ; $r ]
Set Variable [ $r ; Value: MBS("FM.SQL.ReleaseAll") ]

This is what the result looks like:

Unsupported joins in FileMaker

The FileMaker engine for SQL, that the plugin also works with, cannot handle RIGHT JOIN and FULL OUTER JOIN. Therefore, you cannot use them in conjunction with FileMaker. However, in case you encounter them, we will explain both here. RIGHT JOIN is very similar to LEFT JOIN, except that instead of taking all records from the left table, we take them from the right - regardless of whether a corresponding record exists in the left table or not, the fields in the left table that correspond to a record that has no match in the left table are filled with NULL. Another join type is the FULL OUTER JOIN. This includes all records from the left and right table. If there is no match for a record in the opposing table, the records are still transferred to the result set and we also fill them with NULL.

CROSS JOIN

I would also like to mention CROSS JOIN. This join does not use a specific criterion or field, but connects all records in the right table with all records in the left table, regardless of whether the records are matching. So if table A has x entries and table B has y entries, we end up with a result set containing x*y records. We also talk about forming the Cartesian product of these two tables. If we then want to narrow down the records, we can use a WHERE clause. A CROSS JOIN can have the following structure:

SELECT *
FROM 
tabel 1 CROSS JOIN tabel 2

However, another notation is also possible, because we also form the Cartesian product when we combine two tables in the FROM section by listing them one after the other.

SELECT *
FROM tabel 1 ,tabel 2

Today, I would like to give you a little task, which we will solve tomorrow in the door. How would our query

SELECT Rols2.Movie, Movies2.Name, Movies2.Director, Movies2.Launch, Rols2.Actor 
   FROM Movies2 
   JOIN Rols2 ON Movies2.PrimaryKey = Rols2.Movie

look like if we wanted to express it with a CROSS JOIN?

Have fun trying it out, and I hope to see you again tomorrow.


Monkeybread Software Logo with Monkey with Santa hat
5 👈 6 of 24 👉 7
Claris FileMaker Plugin
06 12 25 - 16:27