Skip to content

How do you find the next-higher sequence value, after a known reference value, that also satisfies some other filter condition, in a single SQL query, without using ORDER BY combined with a row-limiting clause?

Answer

Rather than trying to express this as one complex nested subquery with some kind of 'next higher value' pseudo-operator, which doesn't exist in standard SQL, simply combine a plain greater-than comparison with MIN() and your other filter conditions in one straightforward query: SELECT MIN(seq) FROM <table> WHERE seq > <reference_value> AND <other_condition>. MIN() applied over the already-filtered greater-than set directly returns the next qualifying higher value, without needing any special ranking or window-function syntax.

← Browse Reporting and analytics questions