First, join TABLE_2
and TABLE_3
using an inner join and additionally filtering on Value_A = 'a'
:
SELECT
t2.ID_1,
t3.ID_A,
t3.Value_A
FROM
TABLE_2 t2
INNER JOIN TABLE_3 t3 ON t2.ID_A = t3.ID_A
WHERE
t3.Value_A = 'a'
This will give you the following result set:
ID_1 ID_A Value_A
---- ---- -------
1 b a
4 d a
Now use the above as a derived table and join it, using an outer join this time, to TABLE_1
, additionally filtering the results on Value_1 = 11
:
SELECT
t1 .ID_1,
t23.ID_A,
t1 .Value_1,
t23.Value_A
FROM
TABLE_1 t1
LEFT JOIN
(
SELECT
t2.ID_1,
t3.ID_A,
t3.Value_A
FROM
TABLE_2 t2
INNER JOIN TABLE_3 t3 ON t2.ID_A = t3.ID_A
WHERE
t3.Value_A = 'a'
) t23 ON t1.ID_1 = t23.ID_1
WHERE
t1.Value_1 = 11
;
That will give you the output you want:
ID_1 ID_A Value_1 Value_A
---- ---- ------- -------
1 b 11 a
3 NULL 11 NULL
However, nesting a query is not the only way to solve your problem – you can also use a nested join, which is much more concise:
SELECT
t1.ID_1,
t3.ID_A,
t1.Value_1,
t3.Value_A
FROM
TABLE_1 t1
LEFT JOIN
TABLE_2 t2
INNER JOIN TABLE_3 t3 ON t2.ID_A = t3.ID_A AND t3.Value_A = 'a'
ON t1.ID_1 = t2.ID_1
WHERE
t1.Value_1 = 11
;
The last query implements exactly the same logic as the previous query: first, tables TABLE_2
and TABLE_3
are joined and the result is filtered, then it is joined to TABLE_1
and the final set is filtered again. Some people also add brackets around a nested join:
FROM
TABLE_1 t1
LEFT JOIN
(
TABLE_2 t2
INNER JOIN TABLE_3 t3 ON t2.ID_A = t3.ID_A AND t3.Value_A = 'a'
)
ON t1.ID_1 = t2.ID_1
to make it clearer (perhaps both for themselves and for future maintainers) that the nested join takes place before the outer-level one, logically, although the syntax is unambiguous enough without them.
Nevertheless, many people find it confusing even with brackets, and if you find yourself struggling with it as well, there is another option – right outer join:
SELECT
t1.ID_1,
t3.ID_A,
t1.Value_1,
t3.Value_A
FROM
TABLE_2 t2
INNER JOIN TABLE_3 t3 ON t2.ID_A = t3.ID_A AND t3.Value_A = 'a'
RIGHT JOIN TABLE_1 t1 ON t1.ID_1 = t2.ID_1
WHERE
t1.Value_1 = 11
;
Again, the logical sequence of events specified by the FROM clause repeats that of the derived table solution: a join between TABLE_2
and TABLE_3
, filtered, is followed by a join with TABLE_1
. The different syntax does not alter the outcome and the results produced still match your requirements.
Best Answer
If your aim is only delete rows from table1, you can re-write your query as follow:
You convert your
INNER JOIN
between main table (table1) and the others with using ofWHERE EXISTS
condition.Note: string literals (like
'IO'
) need to be quoted with single quotes and not double quotes.