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 - SQL Tutorial
From the course: Complete Guide to SQL for Data Engineering: from Beginner to Advanced
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Introduction to analytical functions5m 31s
-
(Locked)
Using the RANK() function7m 16s
-
(Locked)
Using the DENSE_RANK() function4m 27s
-
(Locked)
Exploring the ROW_NUMBER() function6m 35s
-
(Locked)
Understanding the LAG() and LEAD() functions7m 55s
-
(Locked)
Running totals with SUM() OVER()7m 29s
-
(Locked)
Calculating moving averages7m 33s
-
(Locked)
Challenge: Find out the sum33s
-
(Locked)
Solution: Find out the sum44s
-
(Locked)
-
-
-
-
-
-
-