Explain the purpose of LAG and LEAD functions

SQL
Medium
86.1K views

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.

Start Practicing

Related Interview Questions

Browse all 49 SQL questions