How to find n consecutive numbers in MySql I have numbers starting from 1 to 50 and then 90 to 200 in my database and I want to fetch 100 consecutive numbers then the result should be 90 to 189.
Records in my table are more than 10 million.
Substitute $n with the desired number:

SELECT c1.id 
FROM consecutive c1 
WHERE $n = (SELECT count(*) 
           FROM consecutive c2 
           WHERE c2.id 
           BETWEEN c1.id AND (c1.id + $n - 1)
           );


This does a full table scan for each number, combined with a range scan. It finds all possible combinations (returning the first figure in the chain, so add a LIMIT 1 for better performance if you just want to find the first result. Example:

mysql&gt; CREATE TABLE consecutive (id int PRIMARY KEY);
mysql&gt; INSERT INTO consecutive VALUES (1),(2),(3),(4),(6),(7),(8),(9),(10),(11);
mysql&gt; SELECT c1.id FROM consecutive c1 WHERE 5 = 
       (SELECT count(*) FROM consecutive c2 
        WHERE c2.id BETWEEN c1.id AND (c1.id + 4)
       ) LIMIT 1;
+----+
| id |
+----+
|  6 |
+----+
1 row in set (0.00 sec)


It could probably be optimized to be done in a simple table scan with the help of variables, but this uses simpler and more standard syntax.