Monday, 8 April 2013

JOINS IN SQL


SQL> create table customer(cid int,cname varchar2(20),age int,sex varchar2(10),dob varchar2(20),address varchar2(20),phno int,email varchar2(20),annualincome varchar2(10),primary key(cid));
Table created.
SQL> create table product(cid int,pid int,pname varchar2(20),brandname varchar2(20),price int,quantity int,primary key(cid,pid),foreign key(cid)  references customer);
Table created.
SQL>  insert into customer values('&cid','&cname','&age','&sex','&dob','&address','&phno','&email','
&anualincome');
Enter value for cid: 1
Enter value for cname: john
Enter value for age: 58
Enter value for sex: male
Enter value for dob: 5/7/55
Enter value for address: german
Enter value for phno: 8548547895
Enter value for email: john2@gmail.com
Enter value for annualincome: 15000
1 row created.
SQL> insert into  product  values('&cid','&pid','&pname','&brandname','&price','&quantity');
Enter value for cid: 1
Enter value for pid: 2
Enter value for pname: x220
Enter value for brandname: sony
Enter value for price: 2500
Enter value for quantity: 5
1 row created.

SQL> select * from customer;
CID       CNAME      AGE    SEX       DOB    ADDRESS     PHNO           EMAIL      ANUALINCOM
---------- -------------------- ----------       ---------- -------------------- -------------------- ---------- -----------------      
1             john            58    male       5/7/55   german      8548547895  john2@gmail.com    15000
2             mac            47     male       1/9/65   japan         854769485   mac33@gmail.com   14000
3             sam            21     male       1/7/91   greek         789456215   sam2@yahoo.com     10000

SQL> select * from product;
CID     PID     PNAME     BRANDNAME     PRICE     QUANTITY
------    ------   ------------   ---------------------   ----------    ------------------
1          2           x220         sony                        2500                 5
2          3           xpro          sony                       10000               3
3          4           star 3        samsung                  7000                 1

SQL>select pname,cname from product inner join customer pid=cid;
PNAME     CNAME   
------------   ----------
  xpro              john
  star 3            mac






SQL>select pname,cname from product left join customer pid=cid;
PNAME     CNAME   
------------   ----------
x220           john
xpro            mac
star 3         

SQL>select pname,cname from product right join customer pid=cid;
PNAME     CNAME   
------------   ----------
x220           john
xpro            mac
                   sam

SQL>select pname,cname from product full join customer pid=cid;

PNAME     CNAME   
------------   ----------
x220           john
xpro            mac
star 3          sam 

No comments:

Post a Comment