Explain the purpose of LAG and LEAD functions
This question tests accessing adjacent rows. It evaluates trend analysis skills.
Why Interviewers Ask This
Comparing current row to previous/next is vital for trends. Interviewers check if you know these functions to avoid self-joins.
How to Answer This Question
Define LAG as looking back and LEAD as looking forward. Mention they compare values across rows without self-joins. Give examples like day-over-day change.
Key Points to Cover
- Access adjacent rows
- LAG looks backward
- LEAD looks forward
- Avoids self-joins
Sample Answer
LAG and LEAD are window functions that let you look at values from previous or next rows in the same result set. They are used for comparisons across rows, such as detecting changes from yesterday to today or filling forward values, without needing complex self-joins.
Common Mistakes to Avoid
- Using them outside window context
- Confusing offset directions
- Not specifying default values
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.
Related Interview Questions
Describe a PRIMARY KEY and how it differs from a UNIQUE key
Medium
What is ER model in the DBMS?
Medium
FlipkartWhat is the difference between LIKE and equals operators in SQL?
Easy
TCSWhat is the difference between UNION and UNION ALL?
Easy
What is Join and explain its types?
Medium
FlipkartWhat is the ER model in DBMS?
Easy
Flipkart