Suppose you have a table T(A)with valid only positive integers, for example:
1,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18
In the above example, the result 10. We can always use ORDER BYit DISTINCTto sort and delete duplicates. However, to find the smallest integer not on the list, I came up with the following query SQL:
select list.x + 1
from (select x from (select distinct a as x from T order by a)) as list, T
where list.x + 1 not in T limit 1;
My idea starts with counterand 1, check if this counter is in list: if there is one, return it, otherwise increase and look again. However, I have to run this counter as 1, and then increment. This query works in most cases, since there are some angular cases, for example, in 1. How can I do this in, SQLor do I need a completely different direction to solve this problem?