I have a table like this:
id | val --------- 1 | abc 2 | def 5 | xyz 6 | foo 8 | bar
and type request
SELECT id, val FROM tab WHERE id IN (1,2,3,4,5)
which returns
id | val --------- 1 | abc 2 | def 5 | xyz
Is there a way to make it return NULL on missing identifiers, i.e.
id | val --------- 1 | abc 2 | def 3 | NULL 4 | NULL 5 | xyz
I suppose there should be a complicated LEFT JOIN with me, but I can't wrap my head around it.
EDIT: I see that people think that I want to โfill in the blanksโ in the sequence, but actually I want to replace NULL with the missing values โโfrom the IN list. For example, this
SELECT id, val FROM tab WHERE id IN (1,100,8,200)
must return
id | val --------- 1 | abc 100 | NULL 8 | bar 200 | NULL
In addition, order is not a big deal.
EDIT2: just add a couple of related links:
source share