I currently have a query where it returns the total number of accounts each customer holds, but how do I make it so that it returns only customers who has more than 1 account?
SELECT C.customerID, COUNT(O.accNumber) AS "total"
FROM Customer C, Owns O
WHERE C.customerID = O.customerID
GROUP BY C.customerID
The answer to your question is HAVING
. However, you need to learn to use properJOIN
syntax. Simple rule: Never use a comma in the FROM
clause. Always use explicit JOIN
syntax.
SELECT C.customerID, COUNT(O.accNumber) AS total
FROM Customer C JOIN
Owns O
ON C.customerID = O.customerID
GROUP BY C.customerID
HAVING COUNT(*) > 1;
Actually, you don't even need the JOIN
:
SELECT o.customerID, COUNT(o.accNumber) AS total
FROM Owns o
GROUP BY o.customerID
HAVING COUNT(*) > 1;
That's much simpler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With