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 for…

Common Mistakes to Avoid

  • Using them outside window context
  • Confusing offset directions
  • Not specifying default values

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 101 SQL questions