Bird
0
0

How would you modify this query to exclude products with zero sales from the percent of total calculation?

hard📝 Application Q9 of 15
SQL - Advanced Window Functions
How would you modify this query to exclude products with zero sales from the percent of total calculation?
SELECT product, amount, ROUND(amount * 100.0 / SUM(amount) OVER (), 2) AS pct_total FROM sales;
AAdd a WHERE clause: <code>WHERE amount > 0</code> before the SELECT
BAdd a FILTER clause inside SUM: <code>SUM(amount) FILTER (WHERE amount > 0) OVER ()</code>
CUse CASE inside SUM: <code>SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) OVER ()</code>
DAdd HAVING clause: <code>HAVING amount > 0</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering inside window functions

    WHERE filters rows before window functions, excluding rows from output.
  2. Step 2: Use CASE inside SUM to include zero sales rows but exclude zero amounts from sum

    CASE allows conditional sum without removing rows.
  3. Final Answer:

    Use CASE inside SUM to exclude zero sales from total -> Option C
  4. Quick Check:

    CASE inside SUM filters values without filtering rows [OK]
Quick Trick: Use CASE inside SUM to filter values, keep all rows [OK]
Common Mistakes:
  • Using WHERE filters out rows entirely
  • FILTER clause not supported in all SQL dialects
  • HAVING used without GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes