LEAD, LAG, FIRST_VALUE, LAST_VALUE
These are analytic functions used to access data from different rows in the result set.
Example: Using LEAD and LAG
SELECT Name, Contribution,
LEAD(Contribution) OVER (ORDER BY Name) AS NextContribution,
LAG(Contribution) OVER (ORDER BY Name) AS PreviousContribution
FROM FreedomFighters;
Output:
Shows the next and previous contributions for each freedom fighter.
Notes
LEAD
: Accesses the next row's data.LAG
: Accesses the previous row's data.FIRST_VALUE
andLAST_VALUE
: Retrieve the first and last values in the window.