I have several tables. I'd like to join against. I only want to see records where there is a match on at least one of those tables
insert into foo values
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8);
insert into a values
(1),
(2),
(3);
insert into b values
(3),
(4),
(5),
(6);
desired output:
id id id
1 1 (null)
2 2 (null)
3 3 3
4 (null) 4
5 (null) 5
6 (null) 6
Normally I would do this using WHERE EXISTS (eg: below) but that's not supported in SparkSQL. What is the most performant way to achieve this? I would prefer to rely on my join to determine the result rather than filtering the result set. Also, I am not limited to using SparkSQL, dataframe API is great also.
select *
from foo
left join a on foo.id = a.id
left join b on foo.id = b.id
where exists (select 1 from a x where foo.id = x.id)
or exists (select 1 from b x where foo.id = x.id)
;