SELECT * FROM items i LEFT JOIN groups g ON g.group_id = i.group_id ORDER BY ISNULL(i.group_id) DESC, i.group_id DESC