By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE Foo (
`id` INTEGER,
`number` INTEGER
);
INSERT INTO Foo
(`id`, `number`)
VALUES
('1', '5'),
('2', '8'),
('3', '3'),
('4', '67'),
('5', '1'),
('6', '10');
select f1.*
from Foo f1 inner join Foo f2
on f2.id = f1.id - 1
order by f1.number - f2.number desc limit 1
id | number |
---|---|
4 | 67 |
select f.*
from Foo f
order by number - (select number from Foo where id < f.id order by id desc limit 1) desc
limit 1
id | number |
---|---|
4 | 67 |
select t.id, t.number
from (
select f1.id, f1.number, max(f2.id) previd
from Foo f1 inner join Foo f2
on f2.id < f1.id
group by f1.id, f1.number
) t inner join Foo f
on f.id = t.previd
order by t.number - f.number desc limit 1
id | number |
---|---|
4 | 67 |
select *
from Foo
order by number - lag(number) over (order by id) desc limit 1
id | number |
---|---|
4 | 67 |