2 2 votes Consider the following R A 1 2 3 4 B NULL 1 2 2 select * from R as R1 where not exists (select * from R where B = R1.A) The number of tuple returned by SQL query is ____________ Databases + – srestha 2.6k views answer comment Share Follow Print See all 16 Comments 16 16 Comments reply vijaycs commented Aug 3, 2016 reply Follow flag 2 ?? 1 1 replyShare srestha commented Aug 3, 2016 reply Follow flag yes , tell me approach 0 0 replyShare vijaycs commented Aug 3, 2016 reply Follow flag a[] = { 1, 2, 3, 4} b[]= {null, 1, 2, 2} count=0; for (i=0; i< 4; i++) { flag=1; for ( j=0;j<4 ; j++) { if (a[i]==b[j]) //null== (any not null) -> false..{flag=0;break;} } if(flag) count++; } printf(count) 0 0 replyShare srestha commented Aug 3, 2016 reply Follow flag here it is asking for "select * from R as R1" but I think u r doing "select A from R as R1" 0 0 replyShare vijaycs commented Aug 3, 2016 reply Follow flag no @srestha ... I am counting number of tuples .. see this ... where B = R1.A 0 0 replyShare srestha commented Aug 3, 2016 reply Follow flag In B , here existing NULL In A , here exists 3,4 rt? 0 0 replyShare vijaycs commented Aug 3, 2016 reply Follow flag yes .....so ..?? didn't get you .. didn't you get what i want to say by my code ?? 0 0 replyShare vijaycs commented Aug 3, 2016 reply Follow flag ans = 2 is coming just because of elements 3 and 4 of attribute A.. which have no matching with any element of attribute B. 0 0 replyShare srestha commented Aug 3, 2016 reply Follow flag yes u r taking j inside i and then takes i as output, which is 2 rt? but in this query R and R1 both have A and B separately Now inner query will take R.B=R1.A //which gives only 1 tuple as output, because 3 values of B column is matching with A column And then outer query (not exists) keyword gives output 4-1=3 tuples as output 0 0 replyShare srestha commented Aug 3, 2016 reply Follow flag @vijay that I told in my 1st command , i.e. u r taking A tuple only in consideration 0 0 replyShare srestha commented Aug 3, 2016 reply Follow flag Is there any fault in my logic? 0 0 replyShare vijaycs commented Aug 3, 2016 reply Follow flag Now inner query will take R.B=R1.A //which gives only 1 tuple as output, because 3 values of B column is matching with A column I think in this line you are making mistake ... see ... outer loop - R1 // select * from R as R1 where not exists (select * from R where B = R1.A) inner loop - R both R and R1 are same but R.B=R1.A is doing everything... 0 0 replyShare srestha commented Aug 3, 2016 i edited by srestha Aug 4, 2016 reply Follow flag yes then inner query return from R And outer query will return from R1 A 1 B NULL rt? because in other 3 columns R.B=R1.A 0 0 replyShare srestha commented Aug 4, 2016 reply Follow flag @vijay I am not getting how "select * from R where B = R1.A" is taking 2 tuples? See according to below table it is taking 3 rows. Plz tell me where am I wrong? :( 0 0 replyShare vijaycs commented Aug 4, 2016 reply Follow flag @srestha ... select * from R as R1 where not exists (select * from R where B = R1.A) This query results - All the tuples where the element of attribute A does not have any match with any elements of attribute B of the same relation R. And here element 3 and 4 of attribute A do not have any matching with the all elements of attribute B( NULL, 1, 2, 2). So ans = 2. And if you want to know, how the scanning is being done here, then you can see the above pseudo code written above - And still if you have doubt then take a deep sleep for half an hour and then come back and read all this again .. you will get everything... 0 0 replyShare srestha commented Aug 4, 2016 reply Follow flag Hm, 0 0 replyShare Please log in or register to add a comment.
3 3 votes R1.A R1.B R.A R.B 1 null 1 null 2 1 2 1 3 2 3 2 4 2 4 2 For each row of R1 , (select * from R where B = R1.A) executes...Row from R1 printed when null return from from inner query... For R1.A = 3 and R1.A = 4 there is no matching R.B in any row so for these two row it will return null ...and condition given" doesn't exist" ... So these two row will be printed... papesh answered Aug 3, 2016 • edited Aug 3, 2016 by papesh papesh comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments srestha commented Aug 4, 2016 reply Follow flag cannot understand @Gabbar According to ur table inner query will "select * from R where B = R1.A" R.A R.B 2 1 3 2 4 2 As it have to return whole tuple, not R.A individually it can take. rt? 0 0 replyShare papesh commented Aug 4, 2016 reply Follow flag I think I have made it complex...I'll update in simple way... 0 0 replyShare srestha commented Aug 4, 2016 reply Follow flag No I think the table is all right. 0 0 replyShare Please log in or register to add a comment.
0 0 votes @Srestha. When you compare a number with null, its value may or may not be true. But since, you are not sure if they are equal, you take the comparison to be false. Now, evaluate. Sushant Gokhale answered Sep 25, 2016 Sushant Gokhale comment Share Follow 0 reply Please log in or register to add a comment.