Bird
0
0

You have a table transactions with columns user_id and amount. How do you find the total amount spent by each user?

hard📝 Application Q8 of 15
PostgreSQL - Aggregate Functions and GROUP BY
You have a table transactions with columns user_id and amount. How do you find the total amount spent by each user?
ASELECT user_id, COUNT(amount) FROM transactions GROUP BY amount;
BSELECT SUM(amount) FROM transactions WHERE user_id;
CSELECT user_id, SUM(amount) FROM transactions GROUP BY user_id;
DSELECT user_id, AVG(amount) FROM transactions;
Step-by-Step Solution
Solution:
  1. Step 1: Use SUM to total amounts per user

    SUM(amount) adds amounts for each user.
  2. Step 2: Group results by user_id

    GROUP BY user_id groups transactions by each user.
  3. Final Answer:

    SELECT user_id, SUM(amount) FROM transactions GROUP BY user_id; -> Option C
  4. Quick Check:

    SUM with GROUP BY user_id totals per user [OK]
Quick Trick: Use GROUP BY with SUM to total per group [OK]
Common Mistakes:
  • Grouping by wrong column
  • Using COUNT instead of SUM
  • Missing GROUP BY clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes