Lets say we have 2 tables.
Table Values
Id Group Value
--------------------
A X 15
B Y 55
Table Ranges
Group LowLimit HighLimit
--------------------------------
X 0 10
X 20 30
Y 30 40
Y 50 60
I would like to check which Values.Id has value that falls outside the LowLimit and HighLimit of the group it belongs to in Ranges (notice each group has multiple ranges).
The desired result would be
Table Result
Id
--------------
A (because the value 15 is outside the range of both (0..10) and (20..30)
while B is not part of the result because it is within the range of (50..60) even though it is outside the range of (30..40))
What would the sql would be like?
One option uses an exists query:
Demo
In plain English, this will check each
ID
row fromValues
and check it there does not exist even one range which contains the value. If no such range exists, then this non matchingID
would be reported.