From the course: Complete Guide to SQL for Data Engineering: from Beginner to Advanced

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Understanding the LAG() and LEAD() functions

Understanding the LAG() and LEAD() functions

- [Instructor] Considering our e-commerce database, imagine if we want to analyze whether the demand for a specific product is increasing or decreasing or it is kind of unstable. It can be understand by analyzing the change in total quantity of our successive or the consecutive orders. That's where the LAG and the LEAD function can be applied to track the changes in sequential order data. In technical terms, LAG allows you to access the value of a specific column from the previous row in that specific results end. For example, it could be a very handy when you wanted to compare the current row's value with its preceding row. Like you wanted to check today's stock price with the yesterday's stock price. There is also another function that is the LEAD function that lets you access the value of a specific column from the next row within the result set. LAG get you the previous one, LEAD gets you the next one. So imagine…

Contents