Filtering joins allow us to filter rows from a table based on the presence or absence of matches in another table.
There are two types of filtering joins which are:
These operations are useful in some filtering scenarios and also for debugging when carrying out table join operations ( see Joining).
We wish to return rows of a table that have a match in another table.
In this example, we wish to return rows in table table_1
that match rows in another table table_1
. A row
in table_1
is considered a match to a row in table_1
if the combination of values of columns col_1
and col_2
in both tables are a match.
SELECT t.*
FROM refcon.dataset.table_1 t
INNER JOIN refcon.dataset.table_1_1 t1 ON
t.col_1 = t1.col_1 AND
t.col_2 = t1.col_2
Here is how this works:
INNER JOIN
will only return rows where col_1
and col_2
have the same value in both tables.t.*
to keep only columns from table_1
.Alternatively:
One Join Column
SELECT *
FROM refcon.dataset.table_1
WHERE col_1 IN (SELECT col_1 FROM refcon.dataset.table_1_1)
Here is how this works:
IN
method to identify rows in the table table_1
that have a match
in the table table_1
.col_1
from table_1
in a subquery, and then we apply the IN
operator.WHERE col_1 IN (SELECT col_2 FROM refcon.dataset.table_1_1)
.We wish to return rows of a table that do not have a match in another table.
In this example, we have two tables table_1
and table_1
and we wish to retain rows that are
in table_1
but not in table_1
. A row in table_1
is considered a match to a row in table_1
if
the values of columns col_1
and col_2
in both tables are a match.
SELECT t.*
FROM refcon.dataset.table_1 t
LEFT JOIN refcon.dataset.table_1_1 t1 ON
t.col_1 = t1.col_1 AND
t.col_2 = t1.col_2
WHERE t1.col_1 IS NULL;
Here is how this works:
LEFT JOIN
to join the two tables on col_1
and col_2
.col_1
and col_2
will have NULL
values for all columns coming from table_1.WHERE t1.col_1 IS NULL
which will filter our matching rows.Alternatively:
One Join Column
SELECT *
FROM refcon.dataset.table_1
WHERE col_1 NOT IN (SELECT col_1 FROM refcon.dataset.table_1_1)
Here is how this works:
NOT
operator to take the complement.