Find Missing IDs in a Table

Ref: http://float-middle.blogspot.ca/2009/07/find-missing-value-in-sequence-in.html Suppose you have database table with a unique auto-increment primary key. Over time, entries may be deleted from the table, so there are “holes” in the primary key’s values. Find the missing IDs as follows: SELECT DISTINCT id + 1 FROM mytable WHERE id + 1 NOT IN (SELECT DISTINCT id FROM mytable);