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

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

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

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