0

I am facing some issue while joining multiple tables in oracle. Here are the table structs:

TName : custac
acno - FK (acno custacdetails)
acbal
bid

Tname : custacdetails
custid - FK (custid custdetails)
acno - PK
actype

Tname : custdetails
custid - PK
fname
lname

Tname : branchdetails 
bid
bname

I want to view all customers acno, acbal, branchname, fname, lname whose custid is 11111 and actype is 'SA'

I am using this query but i am getting wrong result

select a.acno,c.fname,c.lname,b.bname,a.acbal 
from branch_details b,
     custac a,
     custacdetails d,
     custdetails c 
where c.custid=11111 
  and a.acno=d.acno  
  and a.branchid=b.bid 
  and actype='SA';
Srijit B
  • 59
  • 1
  • 4
  • 16
  • the problem is that your query is doing a cartesian product, you can resolve including in the WHERE clause a relation with the table custdetails for example (d.custid = c.custid). – Aramillo Aug 22 '14 at 12:42
  • I am using this now. It seems to be fixed. Am i right now? select fname,acbal,a.acno from custdetails cd,custac a,custacdetails d,branch_details b where d.custid=11111 and a.acno=d.acno and d.custid=cd.custid and a.branchid=b.bid; – Srijit B Aug 22 '14 at 12:48
  • Yes that's correct. You got it. – Aramillo Aug 22 '14 at 12:57
  • 1
    Another good example why using explicit `JOIN` operators is better than the (outdated) implicit join in the where clause: you can't forget the join condition. –  Aug 22 '14 at 13:42
  • Thank you Tony891206 & Will Marcouiller. You just cleared a major database concept. I will read more about joing using JOIN. Will get back if I face any problem. :) – Srijit B Aug 22 '14 at 17:02

1 Answers1

0

As per Tony891206 saying, you'Re doing a cartesian product. To avoid it, you need to tell how to join both tables together.

d.custid = c.custid

As follows:

select a.acno,c.fname,c.lname,b.bname,a.acbal 
    from branch_details b,
         custac a,
         custacdetails d,
         custdetails c 
    where d.custid = c.custid
      and c.custid=11111 
      and a.acno=d.acno  
      and a.branchid=b.bid 
      and actype='SA';

All credit goes to Tony891206.

This being said, as stated by a_horse_with_no_name:

Another good example why using explicit JOIN operators is better than the (outdated) implicit join in the where clause: you can't forget the join condition.

So the correct query should be written as follows.

select a.acno, c.fname, c.lname, b.bname, a.acbal
    from     branch_details b
        join custac a        on a.bid    = b.bid
        join custacdetails d on d.acno   = a.acno
        join custdetails c   on c.custid = d.custid
    where   c.custid = 11111
        and d.actype = 'SA'

This last query makes the relationship between the tables glaringly obvious, and allows for room in the where clause to specify only filter criterion.

If you want to know more about SQL syntaxes, please visit the followings:

In short, you would have had better chances to solve your problem by yourself using the SQL-92 join syntax over the SQL-89 one, since you would have had no other choice than to ask yourself how your tables may get joined together while writing the join clauses.

In addition to it, some say that there is a slight improvement on the performance using SQL-92 syntax, which I personally doubt. Besides, I do evangelize SQL-92 join syntax for it is easier to read than the SQL-89.

Community
  • 1
  • 1
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162