Monday, April 12, 2010

Missing Number

In a table you have 99999 rows. But the max id is 100000. That means only one row is missing. How will you find that missing id in one simple query?

















select (max(id)*(max(id)+1))/2 - sum(id) from table;

Math: to find sum of n integers, you can use n*n+1/2.

No comments:

Post a Comment