← Back ← HAVING Top 3 / dept →

LAG — previous row

Window frame: look backward in an ordered partition

SQL drill • Window functions

Table sessions(user_id, login_at) (sorted by time per user). For each row, show the previous login timestamp for the same user (NULL for the first row in each partition).
Same OVER (PARTITION BY … ORDER BY …) skeleton as the top salaries per department drill — good follow-on once partitions feel natural.

Core shape
LAG(col) OVER (PARTITION … ORDER …)
Step 0/6Line
Partitioned ordering → LAG
Press Step or Play.
Ready
LAG reads the prior row in the window order — perfect for gaps, deltas, streaks.