user1121487
user1121487

Reputation: 2680

SQL Server - exclude data where there's no connection to second table

The example below shows the result for every Name that has a connection to Table2 (Table1 TId is PK, and TId in Table2 is the FK).

SELECT T1.Name, T1.Address
FROM Table1 AS T1
    INNER JOIN Table2 AS T2
    ON T1.TId = T2.TId;

I want a list of all Names from Table1 that have NO corresponding row in Table2. The other way around so to speak. How could this be done?

Upvotes: 0

Views: 139

Answers (1)

Akhil
Akhil

Reputation: 7600

You need to use an Outer Join as shown below:

SELECT T1.Name, T1.Address
FROM Table1 AS T1
     LEFT OUTER JOIN Table2 AS T2 ON T1.TId = T2.TId
WHERE T2.TId IS NULL

Upvotes: 2

Related Questions