SQLTeam.com | Weblogs | Forums

Joining Tables


#1

Hi All
I have 2 tables . Table 1 stores information from customers . Table 2 Stores the data for requests that the customers have submitted.

There are 275 customers in the Table 1 for a particular state and only 37 requests that were submitted stored in table 2. I want to write a query such that I get all 275 records from table 1 but request information attached to only that records where there is a request submitted

Below is my query and I'm getting just all records from table 1 mapped to all records in table 2.
Can I get data in such a way that the table 2 fields return null or blank if there is no associated data

SELECT RR.ReqNo, CD.Gname, RD.RptName from customerinfo CD right join REQUESTINFO RR
on CD.MGN = RR.MGN inner join SelectedData RD on RR.Reqno = SD.ReqNo
where CD.state = 'CA'
and RR.Rdate > '12/31/2015'

Sample Data
ReqNo GName RptName
1 abc Demographic Data
2 bca Population Report
Null adc NULL


#2
SELECT ...
FROM customerinfo CD 
LEFT OUTER JOIN REQUESTINFO RR
    ON CD.MGN = RR.MGN AND RR.Rdate > '20151231'
LEFT OUTER JOIN SelectedData SD
    ON RR.Reqno = SD.ReqNo
WHERE CD.state = 'CA'

#3

Thank You Sir. It worked