Link to home
Start Free TrialLog 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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial