Interview Question

What is LEAD()?

LEAD reads a value from a later ordered row.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

LEAD returns a value from a later row in the window ordering. • An offset chooses how many rows ahead to read. • A default can be supplied when no later row exists. • It avoids a self join for many next-row comparisons.

💡 SQL Example

SELECT ordered_at, amount, LEAD(amount) OVER (ORDER BY ordered_at) AS next_amount FROM payments;

Result

Each payment with the following payment amount

⚡ Quick Revision

LEAD reads a value from a later ordered row.