Oracle left outer join query)

Source: Internet
Author: User

When I accidentally executed the left outer join query of Oracle 9i, I found some incredible problems. I will post them here to discuss with you.
Create tables and records to simulate the current situation:
Create Table temp_test1
(
A number (10)

);
Insert into temp_test1 values (1 );
Insert into temp_test1 values (2 );
Insert into temp_test1 values (3 );
Insert into temp_test1 values (4 );
Commit;
 
Create Table temp_test2
(
B Number (10 ),

C varchar2 (2)

);
Insert into temp_test2 values (1, 'A ');
Insert into temp_test2 values (2, 'A ');
Insert into temp_test2 values (3, 'A ');
Insert into temp_test2 values (5, 'B ');
Commit;

 
Let's execute a left outer connection:
-- Expected results
Select a, B, c
From temp_test1 left Outer Join temp_test2 on temp_test1.a = temp_test2. B;
 

The result is as follows:
A B C
1 1 1
2 2 2
3 3 3
4

(Expected results)
OK. Add a filter condition to the backend:
-- Add a condition in temp_test2 (join first, filter later, and a column is missing in the result)
Select a, B, c
From temp_test1 left Outer Join temp_test2 on temp_test1.a = temp_test2. B

Where c = 'a ';

The result is missing:
A B C
1 1 1
2 2 2
3 3 3

(Incorrect results)
From this perspective, we can see that when we connect the left outer table, we first join the two tables and then filter them in the merged result set.
Try another method:
-- Add a condition in temp_test2 and write it in another way (it seems to be filtered and then connected)
Select a, B, c
From temp_test1 left Outer Join temp_test2

On temp_test1.a = temp_test2. B and temp_test2.c = 'a ';

God, this method was originally expected. From this point of view, it seems that the single table filters and reconnects.
If you are not convinced, change the order of the conditions:
Select a, B, c
From temp_test1 left Outer Join temp_test2

On temp_test2.c = 'A' and temp_test1.a = temp_test2. B;

Or the expected results. It seems that the order of the conditions is irrelevant to the results.
Well, it's getting more and more interesting. Use the connection Syntax of sql89 in another way:
-- Use another syntax for left join
Select a, B, c
From temp_test1, temp_test2
Where temp_test1.a = temp_test2. B (+)

The result is the same as that of left Outer Join! (Nonsense)
Try to add superfluous conditions to the image:
-- Use another syntax to connect to the left and add filtering conditions to filter the connection.
Select a, B, c
From temp_test1, temp_test2
Where temp_test1.a = temp_test2. B (+) and temp_test2.c = 'a ';
 

Alas! Incorrect result! Change the order of conditions:
-- Use another syntax for left join, modify the condition order, and filter again after finding or connecting
Select a, B, c
From temp_test1, temp_test2
Where temp_test2.c = 'A' and temp_test1.a = temp_test2. B (+ );
 

The result is still incorrect!
 
From the above experiment, we have obtained the following conclusions:
For left Outer Join In sql92 syntax, the where filter condition is filtered after join;
For the left Outer Join of sql92 syntax, it is different to write the filter condition in the from part and the where part. (why is it different? What is the difference? I can only guess it)
For the left Outer Join (= (+) of sql89 syntax, It is filtered after connection.

 
After testing, right outer join is consistent with left Outer Join.
For such a weird behavior of left Outer Join, I recommend a safer way to force our intention to be reflected in the Syntax:
-- Forcibly filter and connect again
With
Result1
(
Select B, c from temp_test2 where c = 'A'

)
Select a, B, c
From temp_test1 left Outer Join result1 on temp_test1.a = result1. B;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.