Bird
0
0

Which of the following is the correct way to include PARTITION BY in a window function in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Window Functions in PostgreSQL
Which of the following is the correct way to include PARTITION BY in a window function in PostgreSQL?
ASELECT name, SUM(sales) FROM sales_data PARTITION BY region;
BSELECT name, SUM(sales) PARTITION BY region OVER ORDER BY date FROM sales_data;
CSELECT name, SUM(sales) OVER ORDER BY date PARTITION BY region FROM sales_data;
DSELECT name, SUM(sales) OVER (PARTITION BY region ORDER BY date) FROM sales_data;
Step-by-Step Solution
Solution:
  1. Step 1: Syntax of window functions

    Window functions use OVER() with optional PARTITION BY and ORDER BY clauses inside parentheses.
  2. Step 2: Correct placement

    PARTITION BY must appear inside OVER(), before ORDER BY if both are used.
  3. Final Answer:

    SELECT name, SUM(sales) OVER (PARTITION BY region ORDER BY date) FROM sales_data; -> Option D
  4. Quick Check:

    PARTITION BY goes inside OVER() clause [OK]
Quick Trick: PARTITION BY must be inside OVER() clause [OK]
Common Mistakes:
  • Placing PARTITION BY outside the OVER() clause.
  • Swapping ORDER BY and PARTITION BY order inside OVER().
  • Using PARTITION BY as a standalone clause.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes