beginnerWindow Functions — ROW_NUMBER, RANK, LEAD, LAG

What is the difference between LAG() and LEAD()?

Both access values from other rows within the window without self-joins. LAG(col, n, default): returns the value from n rows BEFORE the current row within the partition. LEAD(col, n, default): returns the value from n rows AFTER the current row. Default: value returned when n goes out of partition bounds (default is NULL). Example: SELECT revenue, LAG(revenue, 1, 0) OVER (ORDER BY month_date) AS p

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to You need a sales report showing each sale's amount, the running total, the monthly average, and each salesperson's rank within their region. Can you write this as one query?← Back to all SQL questions