Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3208

An SQL question

$
0
0

I came across a colleague's query that had sub-selects to get the correct result set. Thinking it could be done with a join instead I tried but couldn't get the right results. I tracked it down to the use of a predicate but don't understand why it works the way it does.

 

With the data below,

 

select a.ValCol as "A",b.ValCol as "B",c.ValCol as "C"

from TableA a join TableB b on a.KeyCol = b.KeyCol

                     left outer join TableC c on c.KeyCol = b.KeyCol

where c.Usable = 'Y';

 

gives 3 rows instead of the expected 7.....

 

select a.ValCol as "A",b.ValCol as "B",c.ValCol as "C"

from TableA a join TableB b on a.KeyCol = b.KeyCol

                     left outer join TableC c on c.KeyCol = b.KeyCol and c.Usable = 'Y';

 

... does give the 7 I expect. Can anyone please explain why it works with the condition in the join rather that a where?

 

TIA, Paul

 

create table TableA(KeyCol integer,ValCol char(2));

insert into TableA values(1,'AA');

insert into TableA values(2,'AB');

insert into TableA values(3,'AC');

insert into TableA values(4,'AD');

insert into TableA values(5,'AE');

insert into TableA values(6,'AF');

insert into TableA values(7,'AG');

insert into TableA values(8,'AH');

insert into TableA values(9,'AI');

insert into TableA values(10,'AJ');

 

create table TableB(KeyCol integer,ValCol char(2));

insert into TableB values(1,'BA');

insert into TableB values(2,'BB');

insert into TableB values(4,'BD');

insert into TableB values(6,'BF');

insert into TableB values(7,'BG');

insert into TableB values(8,'BH');

insert into TableB values(10,'BJ');

 

create table TableC(KeyCol integer,ValCol char(2),Usable char(1));

insert into TableC values(1,'CA','Y');

insert into TableC values(4,'CD','Y');

insert into TableC values(6,'CF','N');

insert into TableC values(8,'CH','Y');


Viewing all articles
Browse latest Browse all 3208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>