Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL query issue -- homework

Tags:

mysql

I need some help with one of the questions from my homework, Ive been trying for about an hour now and cant get it to run.

List the customers that bought more items than the average number of items per customer

The tables are as follows:

Customer(Cnum, CustomerName, Address)  
Item(Inum, ItemName, Manufacturer, Year)  
Bought(Cnum, Inum, Date, Quantity)   
Prefer(Inum, Cnum)   

The best i could figure out was that it needs to be the total Quantity per Customer compared to the overall average of the Quantity. I've tried various forms of this query:

SELECT Cnum
FROM Bought
WHERE
(
    SELECT Cnum, SUM(Quantity)
    FROM Bought
    GROUP BY Cnum;
) >
(
    SELECT AVG(Quantity)
    FROM Bought
);

But it returns an error -- (phpMyAdmin isnt telling me what the problem is, just failing to execute and going to no connection page, which means error in my query)

I have also tried to return the higher SUM with:

SELECT SUM(Quantity)
FROM Bought
WHERE SUM(Quantity) > AVG(Quantity);

And same issue.

Any help would be appreciated, even an explanation as to why the second one fails.

like image 942
NaGeLxZ Avatar asked Dec 05 '25 02:12

NaGeLxZ


1 Answers

You might want to take a look at the HAVING clause of SQL.

Note: I'm intentionally not giving you the full answer since this is homework.

like image 182
JohnFx Avatar answered Dec 07 '25 18:12

JohnFx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!