Efficiently joining over interval ranges in SQL
问题 Suppose I have two tables as follows (data taken from this SO post): Table d1 : x start end a 1 3 b 5 11 c 19 22 d 30 39 e 7 25 Table d2 : x pos a 2 a 3 b 3 b 12 c 20 d 52 e 10 The first row in both tables are column headers. I'd like to extract all the rows in d2 where column x matches with d1 and pos1 falls within (including boundary values) d1 's start and end columns. That is, I'd like the result: x pos start end a 2 1 3 a 3 1 3 c 20 19 22 e 10 7 25 The way I've seen this done so far is: