Q:

Ignoring a column when joined column has more than one value

I have a table, Orgs, that contains the following data:
ID   Name            AddressID
0    McDonalds          4
1    Starbucks          5
2    Burger King        7

I also have a table, Addresses, that contains the following data:
ID   OrgID   Address
3     0      123 Main St.
4     0      456 East Ave.
5     1      789 Young St.
6     2      5 Riverside Dr.
7     2      8 Lakeview Ave.

I need to create a select statement that selects the names of companies from Orgs table and their address. A company in the Orgs table can have 1 or more addresses. If the company only has one address, I don't want to return any address for that company. If the company has more than one address, I want to return the address where Orgs.AddressID = Addresses.ID
So my results should look like this:
ID   Name         Address
0    McDonalds    456 East Ave.
1    Starbucks
2    Burger King  8 Lakeview Ave.

I'm not sure how to do this. Thanks for your help!

A:

SELECT 
    O.ID, O.Name, A.Address
FROM 
    Orgs O
LEFT OUTER JOIN 
    (SELECT OrgID FROM Addresses GROUP BY OrgID HAVING COUNT(*) > 1) X ON O.ID = X.OrgID
LEFT OUTER JOIN 
    Addresses A ON X.OrgID = A.OrgID AND O.AddressID = A.ID

