Bird
0
0

Which of the following SQL statements correctly uses LEAD to retrieve the next price ordered by product_id?

easy📝 Syntax Q3 of 15
SQL - Advanced Window Functions
Which of the following SQL statements correctly uses LEAD to retrieve the next price ordered by product_id?
ASELECT product_id, price, LEAD(price) OVER () AS next_price FROM Products;
BSELECT product_id, price, LEAD(price) FROM Products ORDER BY product_id;
CSELECT product_id, price, LEAD(price) AS next_price FROM Products ORDER BY product_id;
DSELECT product_id, price, LEAD(price) OVER (ORDER BY product_id) AS next_price FROM Products;
Step-by-Step Solution
Solution:
  1. Step 1: Syntax of LEAD

    The LEAD function requires an OVER() clause with an ORDER BY to define the row order.
  2. Step 2: Analyze options

    SELECT product_id, price, LEAD(price) OVER (ORDER BY product_id) AS next_price FROM Products; correctly uses LEAD(price) OVER (ORDER BY product_id). SELECT product_id, price, LEAD(price) FROM Products ORDER BY product_id; misses OVER clause. SELECT product_id, price, LEAD(price) AS next_price FROM Products ORDER BY product_id; misses OVER clause and misplaces ORDER BY. SELECT product_id, price, LEAD(price) OVER () AS next_price FROM Products; uses OVER() without ORDER BY, which is invalid for LEAD.
  3. Final Answer:

    SELECT product_id, price, LEAD(price) OVER (ORDER BY product_id) AS next_price FROM Products; -> Option D
  4. Quick Check:

    LEAD needs OVER with ORDER BY [OK]
Quick Trick: LEAD requires OVER with ORDER BY clause [OK]
Common Mistakes:
  • Omitting OVER clause
  • Using ORDER BY outside OVER
  • Not specifying order in OVER

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes