Change from the previous order
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
The task
For every paid order, return the id and the difference between its amount and the amount of the previous paid order, ordered by order date then id. The first order shows NULL.
What this exercise teaches
LAG and LEAD look backwards and forwards without a self join.
A hint if you get stuck
LAG(amount) OVER (ORDER BY ...) reads the previous row.
This exercise is part of Pro
Pro unlocks all 60 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.