Link to home
Create AccountLog in
Avatar of GordonPrince
GordonPrinceFlag for United States of America

asked on

return Y or N based on result of EXISTS subquery

What would be a good way to make this logic work in a SQL query?

select col1, col2, col3, YorN
from tab1

I want to return YorN as either a literal 'Y' or 'N' based on whether or not something EXISTS or not.

if EXISTS (select 1 from tab2 where tab1.col1 = tab2.col1) then Y
if NOT EXISTS (same as above) then select N

What's the most efficient way of doing this?
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Something like this?

Lee
if EXISTS (select top 1 * from tab2 where tab1.col1 = tab2.col1)
    select 'Y'
else
    select 'N'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ChadFolden
ChadFolden

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer