Saturday, 8 October 2011

Joins In SQL


data cust_house;
input acno hm_ln $ year amount;
cards;
110010 lh023 2006 500000
110011 lh024 2006 700000
110012 lh045 2006 250000
110013 lh047 2007 300000
110014 lh057 2007 500000
;
data cust_vech;
input acno vc_ln $ year amount;
cards;
110023 lv001 2006 450000
110126 lv002 2006 300000
110012 lv012 2007 400000
110278 lvo45 2007 300000
110014 lv046 2007 400000
110345 lvo56 2007 300000
;
/*both houseloan and vehicle loan*/
proc sql;
select * from cust_house,cust_vech where cust_house.acno=cust_vech.acno;
quit;
/*customization*/
proc sql;
select h.acno,h.hm_ln,h.year,h.amount,
v.vc_ln,v.year,v.amount
from cust_house as h,cust_vech as v where cust_house.acno=cust_vech.acno;
quit;
/*inner join*/
proc sql;
select * from cust_house inner join cust_vech on cust_house.acno=cust_vech.acno;
quit;
/*loan taken in same year*/
proc sql;
select * from cust_house inner join cust_vech on
cust_house.acno=cust_vech.acno and
cust_house.year=cust_vech.year;
quit;
/*leftjoin*/
proc sql;
select * from cust_house left join cust_vech on cust_house.acno=cust_vech.acno;
quit;
/*right join*/
proc sql;
select * from cust_house right join cust_vech on cust_house.acno=cust_vech.acno;
quit;
/*full join*/
proc sql;
select * from cust_house full join cust_vech on cust_house.acno=cust_vech.acno;
quit;

No comments:

Post a Comment