Bird
0
0

Given the table sales_data with columns product and amount, what is the output of this query?

medium📝 query result Q13 of 15
SQL - Advanced Window Functions
Given the table sales_data with columns product and amount, what is the output of this query?
SELECT product, amount, ROUND((amount / SUM(amount) OVER ()) * 100, 1) AS percent_total FROM sales_data ORDER BY product;

Assume the data:
product | amount
-------|-------
A | 50
B | 30
C | 20
A[{"product": "A", "amount": 50, "percent_total": 100.0}, {"product": "B", "amount": 30, "percent_total": 100.0}, {"product": "C", "amount": 20, "percent_total": 100.0}]
B[{"product": "A", "amount": 50, "percent_total": 33.3}, {"product": "B", "amount": 30, "percent_total": 20.0}, {"product": "C", "amount": 20, "percent_total": 13.3}]
C[{"product": "A", "amount": 50, "percent_total": 25.0}, {"product": "B", "amount": 30, "percent_total": 15.0}, {"product": "C", "amount": 20, "percent_total": 10.0}]
D[{"product": "A", "amount": 50, "percent_total": 50.0}, {"product": "B", "amount": 30, "percent_total": 30.0}, {"product": "C", "amount": 20, "percent_total": 20.0}]
Step-by-Step Solution
Solution:
  1. Step 1: Calculate total amount

    Total = 50 + 30 + 20 = 100.
  2. Step 2: Calculate percent for each product

    Percent = (amount / total) * 100 rounded to 1 decimal:
    A: (50/100)*100 = 50.0
    B: (30/100)*100 = 30.0
    C: (20/100)*100 = 20.0
  3. Final Answer:

    [{"product": "A", "amount": 50, "percent_total": 50.0}, {"product": "B", "amount": 30, "percent_total": 30.0}, {"product": "C", "amount": 20, "percent_total": 20.0}] -> Option D
  4. Quick Check:

    Percent totals sum to 100% [OK]
Quick Trick: Sum amounts, divide each by total, multiply by 100 [OK]
Common Mistakes:
  • Not rounding results
  • Using COUNT() instead of SUM()
  • Calculating percent incorrectly (e.g., reversed division)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes