Drop your answer before I hit depth 👇 This one trips up a lot of people. For the earliest year in the dataset, there’s no prior row for LAG() to look back at, so it returns NULL. Not 0, not an error, but NULL. And if you need a fallback value, LAG() actually has a built-in third argument for that: LAG(COUNT(*), 1, 0) gives you 0 instead of NULL. Window functions on aggregates can be next-level SQL, but knowing the edge cases is what separates good from great. 💾 Save this for your next interview and tag a data person who’d get this wrong. P.S. If you need to perform this type of operation or those involving window functions, in general, and the data will be consumed in a dashboard or view sitting on top of a BI tool, then it is highly likely the calculation should be in that layer and not in SQL. #sql #windowfunctions #dataengineering #datascience

If you're ever feeling mentally drained or stuck on a problem, try exercising or walking outside. You may be surprised by the number of solutions that come to you while doing so.

Like
Reply

All I did was guesstimate the weight of the yellow plate. I think 10kg🙂

NULL from LAG at earliest row. Classic interview trap 🙌Chris Perry

See more comments

To view or add a comment, sign in

Explore content categories