with recursive free_icons (id) as (
select 1
union all
select id+1 from free_icons where id <= 40
)
select * from free_icons
where not exists (
select icon_id from icons where icon_id = free_icons.id
)
limit 1;
select icons.icon_id + 1
from icons
left join icons as next_icon on icons.icon_id + 1 = next_icon.icon_id
where next_icon.icon_id is null and icons.icon_id between 2 and 40
limit 1;