clear markdown compare help donate comments/suggestions/bugs a leap of faith? diddy dollings
clear markdown donate comments/suggestions/bugs a leap of faith?
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 432228 distinct fiddles created so far.

create table tbl (Id int, ChildId int); insert into tbl values (1, NULL), (2, 3), (3, NULL), (4, 1), (5, NULL), (6, NULL);
6 rows affected
 hidden batch(es)


select case when t2.id is not null then t2.id else t1.id end as id, case when t2.id is not null then t2.childid else t1.childid end as childid from tbl t1 left join tbl t2 on t2.childid = t1.id
id childid
4 1
2 3
2 3
4 1
5
6
 hidden batch(es)