My tables are as follows:
SELECT * FROM r_a;
CLASS SECTION
----- -------
1 A
1 B
2 A
2 B
SELECT * FROM r_b;
CLASS SECTION
----- -------
1 B
1 C
2 B
2 C
I want to perform a full outer join on these tables over the column SECTION
where CLASS=1
in both tables. My desired output is :
SECTION SECTION_1
------- ---------
B B
A (Null)
(Null) C
However, the following query yields only the matching row, similar to result of an inner join.
**QUERY 1**
SELECT a.section, b.section
FROM
r_a a
FULL OUTER JOIN
r_b b
ON a.section=b.section
WHERE A.class=1 AND B.class=1
SECTION SECTION_1
------- ---------
B B
I am able to achieve desired result by taking the where conditions inside a nested query:
**QUERY 2**
SELECT a.section, b.section
FROM
(SELECT SECTION FROM r_a WHERE class=1) a
FULL OUTER JOIN
(SELECT SECTION FROM r_b WHERE class=1) b
ON a.section=b.section
SECTION SECTION_1
------- ---------
B B
(Null) C
A (Null)
The result is more surprising when the where
conditions in **Query 1**
is moved to the on
clause:
** Query 3**
SELECT a.section, b.section
FROM
r_a a
FULL OUTER JOIN
r_b b
ON a.section=b.section AND A.class=1 AND B.class=1
SECTION SECTION_1
------- ---------
B B
(Null) C
(Null) B
(Null) C
B (Null)
A (Null)
A (Null)
MORE CLARITY:
*************
SELECT *
FROM
r_a a
FULL OUTER JOIN
r_b b
ON a.section=b.section AND A.class=1 AND B.class=1
CLASS SECTION CLASS_1 SECTION_1
------ ------- ------- ---------
1 B 1 B
(Null) (Null) 1 C
(Null) (Null) 2 B
(Null) (Null) 2 C
2 B (Null) (Null)
2 A (Null) (Null)
1 A (Null) (Null)
Explanation on why Query 1
doesn't produce the desired result and why Query 3
joins on class=2
even when the on
clause says AND A.class=1 AND B.class=1
is greatly appreciated.