Assuming no correlation is needed, use:
SELECT a.* FROM A a WHERE EXISTS(SELECT NULL FROM B b HAVING MIN(b.some_val) > a.val)
If you need correlation:
SELECT a.* FROM A a WHERE EXISTS(SELECT NULL FROM B b WHERE b.id = a.id HAVING MIN(b.some_val) > a.val)
Description
EXISTS is evaluated on a boolean based on the first match — this makes it faster than using IN, and — unlike using JOIN — will not duplicate rows. The SELECT part does not matter - you can change it to EXISTS SELECT 1/0 ... and the query will still work, despite the obvious division by zero error.
The subquery in EXISTS uses the aggregate function MIN to get the smallest value B.some_val - if this value is greater than a.val, a.val is less than all b. The only need for a WHERE is for correlation — aggregate functions can only be used in a HAVING .
source share