Search code examples
sqlpostgresqlcountsubquery

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


Solution

  • 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;