Find Missing IDs in a Table

Ref: 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);