The description you have posted raises a lot of questions that need to be answered - In the query below, I have made many assumptions and indicated those assumptions:INSERT INTO TableC(UniqueId, ParentId,ItemId, SortOrder)SELECT DISTINCT TOP (5) -- do you want distinct on all, or only on some columns? a.UniqueId, a.ParentId, b.ItemId, 0 AS SortOrder -- What is the criterion for deciding the sort order?FROM TableA a INNER JOIN B ON -- you may need LEFT or OUTER JOIN depending on your requirements a.SummaryId = b.SummaryId -- do you also need to join on Quantity?WHERE NOT EXISTS ( SELECT * FROM TableC c WHERE c.UniqueId = a.UniqueId AND b.ItemId = c.ItemId -- do you need this join also? )--ORDER BY -- How do you decide the TOP 5? what should be the sorting order?-- a.ParentId