×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: SQL
Posted by: Denny Kluge
Added: Mar 20, 2017 8:41 AM
Views: 15
Tags: sql
How to find a gap in the ID column in a table. Source: http://stackoverflow.com/a/1312137
  1. SELECT  *
  2. FROM    (
  3.         SELECT  id + 1 AS gap
  4.         FROM    mytable mo
  5.         WHERE   NOT EXISTS
  6.                 (
  7.                 SELECT  NULL
  8.                 FROM    mytable mi
  9.                 WHERE   mi.id = mo.id + 1
  10.                 )
  11.         ORDER BY
  12.                 id
  13.         )
  14. WHERE   rownum = 1