-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28_join_wth_exclusion.sql
58 lines (49 loc) · 1.12 KB
/
28_join_wth_exclusion.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--Join with exclusion
/*
--store_a table
create table store_a(
a_id int,
name varchar(32)
)
insert into store_a(a_id,name)
values(1,'apple'),
(2,'orange'),
(3,'tomato'),
(4,'mango')
--store_b table
create table store_b(
b_id int,
name varchar(32)
)
insert into store_b(b_id,name)
values(8,'apple'),
(9,'banana'),
(10,'mango'),
(11,'watermelon')
*/
select * from store_a
select * from store_b
--Question?
--fruits that are only available in store_a ?
--result set should contain the name of fruit ('name' column)
--fruits that are only available in store_b ?
--result set should contain the name of fruit ('name' column)
--fruits that are exculsive to a store ?
--result set should contain the name of fruit ('name' column)
select a.name
from store_a a
left join store_b b
on a.name = b.name
where b.name is null
select b.name
from store_a a
right join store_b b
on a.name = b.name
where a.name is null
select (case when a.name is not null then a.name
else b.name end) as name
from store_a a
full outer join store_b b
on a.name = b.name
where a.name is null
or b.name is null