Help Me Understand Why SELECT(*) Decides Whether and Answer is Right or Wrong


#1

I’m working on Unit 2 in SQL From Scratch, with the following information and question:

The payments table has the following columns: id, user_id, amount, status, pay_date.

Find all the users that have successfully paid Codeflix and their total payments.
Sort them by their total payments (from high to low).
Use SUM(), GROUP BY, and ORDER BY.

The correct answer is:

SELECT user_id, SUM(amount)
FROM payments
WHERE status = ‘paid’
GROUP BY user_id
ORDER BY SUM(amount) DESC;

I couldn’t understand why I was getting all the information it was asking for, yet it was marking it as wrong with the syntax below:

SELECT user_id, SUM(amount), COUNT(*)
FROM payments
WHERE status = ‘paid’
GROUP BY user_id
ORDER BY SUM(amount) DESC;

Why is it that when I additionally use COUNT(*) it marks it as incorrect? As far as I’m aware it looked like the results were the exact same, with the exception of an additional column with more information. In previous problems I’ve created more information than was asked for and was marked correct.


#2

Update:
Perhaps I’ve figured this out myself. Because both are aggregate functions, the SUM argument is stating to SUM all of the payments, while COUNT(*) is listing each row/line item as a single payment. So, if there are more than one payment a user’s total would not be sum’d and would be broken down in a more granular fashion.

Can someone confirm this?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.