Skip to content
Advertisement

Sql check range exist between a range

i have a table like

----------------------------------
|  ID  |  MinValue  |  MaxValue  |
----------------------------------
|  1   |      0     |    10      |
|  2   |     11     |    20      |
|  3   |     21     |    30      |

and i have a range value for example

minV = 5 
maxV = 15

i want to get row who’s value matchs the range from database table

Example minV = 5 , maxV = 15 should return first and second record

Example minV = 5 , maxV = 35 should return all record

Example minV = 5 , maxV = 9 should return first record

Advertisement

Answer

I think you want:

select t.*
from t
where @minv <= t.maxvalue and
      @maxv >= t.minvalue

This is the logic for overlapping intervals — basically, each “min” is less than the other’s “max”.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement