Skip to content
Advertisement

check if value is available in DB or not

I have a table which has the schema:

id   |  name 
1    | AD
2    | BC

i need a query for the report which contains whether the name i give exists in the DB .

name | isExists
AD   | yes
BC   | yes
CA   | NO

the name AD,BC,CA should be given by me and its not in any other table.

Thanks in advance

Advertisement

Answer

Use an outer join against a values list:

select v.name, t.name is not null as does_exist
from (
   values ('AD'), ('BC'), ('CA')
) as v(name)
  left join the_table t on t.name = v.name;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement