Bird
0
0

You want to create a view that shows the total sales per customer from the Sales table with columns customer_id and amount. Which SQL correctly creates this view?

hard📝 Application Q8 of 15
SQL - Views
You want to create a view that shows the total sales per customer from the Sales table with columns customer_id and amount. Which SQL correctly creates this view?
ACREATE VIEW CustomerSales AS SELECT customer_id, amount FROM Sales;
BCREATE VIEW CustomerSales AS SELECT customer_id, SUM(amount) AS total FROM Sales GROUP BY customer_id;
CCREATE VIEW CustomerSales AS SELECT customer_id, COUNT(amount) FROM Sales;
DCREATE VIEW CustomerSales AS SELECT customer_id, SUM(amount) FROM Sales;
Step-by-Step Solution
Solution:
  1. Step 1: Identify aggregation needed

    You want total sales per customer, so SUM(amount) grouped by customer_id is required.
  2. Step 2: Check each option

    CREATE VIEW CustomerSales AS SELECT customer_id, SUM(amount) AS total FROM Sales GROUP BY customer_id; uses SUM(amount) with GROUP BY customer_id correctly; others miss grouping or use wrong aggregation.
  3. Final Answer:

    CREATE VIEW CustomerSales AS SELECT customer_id, SUM(amount) AS total FROM Sales GROUP BY customer_id; -> Option B
  4. Quick Check:

    Aggregation with GROUP BY needed = CREATE VIEW CustomerSales AS SELECT customer_id, SUM(amount) AS total FROM Sales GROUP BY customer_id; [OK]
Quick Trick: Use GROUP BY with aggregation in views for summaries [OK]
Common Mistakes:
MISTAKES
  • Omitting GROUP BY with aggregation
  • Using COUNT instead of SUM for totals
  • Not aliasing aggregated columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes