Bird
0
0

Given a table Payments with columns UserID, Amount, and PaymentDate, write a query to find the total amount paid by each user in the year 2023.

hard📝 Conceptual Q9 of 15
SQL - Aggregate Functions
Given a table Payments with columns UserID, Amount, and PaymentDate, write a query to find the total amount paid by each user in the year 2023.
ASELECT UserID, SUM(Amount) FROM Payments WHERE YEAR(PaymentDate) = 2023;
BSELECT UserID, SUM(Amount) FROM Payments WHERE PaymentDate BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY UserID;
CSELECT UserID, SUM(Amount) FROM Payments HAVING PaymentDate BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY UserID;
DSELECT UserID, SUM(Amount) FROM Payments GROUP BY UserID WHERE PaymentDate >= '2023-01-01';
Step-by-Step Solution
Solution:
  1. Step 1: Filter payments in 2023 using WHERE and BETWEEN

    Use WHERE with BETWEEN to select dates from 2023-01-01 to 2023-12-31.
  2. Step 2: Group by UserID and sum Amount

    GROUP BY UserID groups payments per user; SUM calculates total amount.
  3. Final Answer:

    SELECT UserID, SUM(Amount) FROM Payments WHERE PaymentDate BETWEEN '2023-01-01' AND '2023-12-31' GROUP BY UserID; -> Option B
  4. Quick Check:

    Filter dates with WHERE before grouping [OK]
Quick Trick: Use WHERE with BETWEEN for date filtering [OK]
Common Mistakes:
MISTAKES
  • Placing WHERE after GROUP BY
  • Using HAVING for row filtering
  • Ignoring date range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes