By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE foo (id INT, num INT)
SELECT 1 id , 5 num UNION ALL
SELECT 2 , 8 UNION ALL
SELECT 3 , 3 UNION ALL
SELECT 4 , 67 UNION ALL
SELECT 5 , 1 UNION ALL
SELECT 6 , 65 UNION ALL
SELECT 7 , 10;
SELECT * FROM foo;
id | num |
---|---|
1 | 5 |
2 | 8 |
3 | 3 |
4 | 67 |
5 | 1 |
6 | 65 |
7 | 10 |
SELECT t2.id, t2.num
FROM foo t1
JOIN foo t2 ON t1.id < t2.id
WHERE NOT EXISTS ( SELECT NULL
FROM foo t3
WHERE t1.id < t3. id
AND t3.id < t2.id )
GROUP BY t2.id, t2.num
ORDER BY t2.num - t1.num DESC, t2.id LIMIT 1;
id | num |
---|---|
4 | 67 |