Join Tables On
Description
The JOIN
statement joins two tables ON
a defined column. It is a regular JOIN
used throughout SQL.
Syntax
Here is the syntax:
On execution, we get:
Where:
Name | Description |
---|---|
t1.column_name | Name of the column from the first table. |
t2.column_name | Name of the column from the second table. |
integration_name.table_name | Name of the table used in the JOIN operation. |
Nested JOINs
MindsDB provides you with two categories of JOINs
. One is the JOIN
statement which combines the data table with the model table in order to fetch bulk predictions. Another is the regular JOIN
used throughout SQL. Please note that only the latter one requires the ON
clause.
You can nest these types of JOINs
as follows:
Example 1
Let’s use the following data to see how the different types of JOINs
work.
The pets
table that stores pets:
And the owners
table that stores pets’ owners:
JOIN
or INNER JOIN
The JOIN
or INNER JOIN
command joins the rows of the owners
and pets
tables wherever there is a match. For example, a pet named Lake does not have an owner, so it’ll be left out.
On execution, we get:
As in standard SQL, you can use the WHERE
clause to filter the output data.
On execution, we get:
LEFT JOIN
The LEFT JOIN
command joins the rows of two tables such that all rows from the left table, even the ones with no match, show up. Here, the left table is the owners
table.
On execution, we get:
RIGHT JOIN
The RIGHT JOIN
command joins the rows of two tables such that all rows from the right table, even the ones with no match, show up. Here, the right table is the pets
table.
On execution, we get:
FULL JOIN
or FULL OUTER JOIN
The FULL [OUTER] JOIN
command joins the rows of two tables such that all rows from both tables, even the ones with no match, show up.
On execution, we get:
Example 2
More than two tables can be joined subsequently.
Let’s use another table called animals
:
Now we can join all three tables.
On execution, we get: