drop table #A
drop table #B
select 1 ID, 'AAA' colA, 'BBBBB' colB into #A
union
select 3, 'CCC', 'DDDDD'
union
select 5, 'DDD', 'FFF'
union
select 8, 'EEE', 'FFFFF'
select 2 ID, 'aaa' colX, 'bbbbb' colY into #B
union
select 4, 'ccc', 'ddddd'
union
select 5, 'sssss', 'd'
union
select 6, 'ffff', 'ddd'
union
select 7, 'adf', 'fghs'
union
select 9, 'ewt', 'qt'
select * from #A
select * from #B
select B.ID, colA, colB, colX, colY
from #A A,
(
select *, (select top 1 ID from #A where ID <= #B.ID order by ID desc) aID
from #B
) B
where A.ID = B.aID
union
select ID, colA, colB, null, null
from #A
where ID not in (select ID from #B)
drop table #B
select 1 ID, 'AAA' colA, 'BBBBB' colB into #A
union
select 3, 'CCC', 'DDDDD'
union
select 5, 'DDD', 'FFF'
union
select 8, 'EEE', 'FFFFF'
select 2 ID, 'aaa' colX, 'bbbbb' colY into #B
union
select 4, 'ccc', 'ddddd'
union
select 5, 'sssss', 'd'
union
select 6, 'ffff', 'ddd'
union
select 7, 'adf', 'fghs'
union
select 9, 'ewt', 'qt'
select * from #A
select * from #B
select B.ID, colA, colB, colX, colY
from #A A,
(
select *, (select top 1 ID from #A where ID <= #B.ID order by ID desc) aID
from #B
) B
where A.ID = B.aID
union
select ID, colA, colB, null, null
from #A
where ID not in (select ID from #B)