Introduction
The LEAD function helps you look at the next row's data in a list without moving your place. It makes comparing current and next items easy.
LEAD(column_name, offset, default_value) OVER (ORDER BY column_to_order)
SELECT LEAD(salary) OVER (ORDER BY employee_id) AS next_salary FROM employees;
SELECT LEAD(sales, 2, 0) OVER (ORDER BY sales_date) AS sales_after_two_days FROM sales_data;
SELECT product, price, LEAD(price) OVER (ORDER BY price) AS next_price FROM products;
CREATE TABLE daily_sales (day INT, sales INT); INSERT INTO daily_sales VALUES (1, 100), (2, 150), (3, 120), (4, 130); SELECT day, sales, LEAD(sales) OVER (ORDER BY day) AS next_day_sales FROM daily_sales ORDER BY day;
LEAD function do in a query?LEAD function to get the next row's salary ordered by employee_id?Employees with columns id and salary sorted by id, what will be the output of this query?SELECT id, salary, LEAD(salary) OVER (ORDER BY id) AS next_salary FROM Employees;
id | salary
1 | 5000
2 | 6000
3 | 5500LEAD:SELECT id, salary, LEAD(salary, 2) ORDER BY id FROM Employees;
hire_date. If no such row exists, show 0 instead of NULL. Which query correctly uses LEAD to achieve this?Pass the test and this pattern moves unknown