How to return NULL from a select-where-in postgresql query when the record does not exist in the array?

I have a query

select * 
from table1 as t
where t.id in ('1', '2')

If both records are the my result is [1, 2].
If only record #1 is there my result is [1].

But if only record #1 is there how can I make the result be [1, NULL] ?

>Solution :

Using VALUES and LEFT JOIN:

SELECT t.id
FROM (VALUES ('1'), ('2')) s(c)
LEFT JOIN table1 AS t
  ON t.id = s.c;

db<>fiddle demo

Leave a Reply