How to get data for all the values mentioned in In condition

Viewed 31

I have certain values like (T1,T2,T3,T4).

I'm using these in an IN condition to get a data from SQL Server:

SELECT * 
FROM TAble 
WHERE Key IN ('T1', 'T2', 'T3', 'T4')

I have 3 entries like below

T1   34  
T2   67  
T3   76

I need T4 has to be included with NULL entry. Is that possible?

1 Answers

Yes, it's possible - using a table valued constructor:

SELECT * 
FROM (VALUES ('T1'), ('T2'), ('T4'), ('T3')) AS [Keys]([Key])
LEFT JOIN [TAble] As T 
     ON [Key] = [Keys].[Key]
Related