Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. LAG and LEAD

SQL · Window Functions

LAG and LEAD

Mediumsql-19
lagleadwindow

Question

What do LAG and LEAD do?

Solution

LAG() gets a value from a previous row.

LEAD() gets a value from a future row.

Example:

LAG(event_time) OVER (
    ORDER BY event_time
)

gets the previous event time.

Then:

event_time - LAG(event_time) OVER (...)

can calculate the time between events.

LEAD() is useful when comparing the current event with the next event.

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext