Compare to Row
Cats are fickle. Each cat would like to lose weight to be the equivalent weight of the cat weighing just less than it.
Print a list of cats, their weights and the weight difference between them and the nearest lighter cat ordered by weight.
Return: name, weight, weight_to_lose
Order by: weight Show Table Schema
Correct output but can you use 'lag'?
lag() lets us compare to the previous rows [and lead() the next rows] [min() also works]
It is useful for looking for strange step ups/downs in data
Fun Fact: Lag can take a default. But it must be the same type. For doubles this means explicitly casting it as double precision. next question
select name, weight, coalesce(weight - lag(weight, 1) over (order by weight), 0) as weight_to_lose FROM cats order by weight