4

Im having a little trouble trying with the count function in SQL.

What I am attempting is to count the amount of people with a certain name

For example:

Name | Number
==============
Ivan | 9      
Brody| 8
Ray  | 7

And where there are the same instance sort alphabetically.

Any help is much appreciated!

1 Answer 1

5

When using COUNT() which is an AGGREGATE FUNCTION it needs to have GROUP BY Clause. Try this,

SELECT Name, COUNT(name)
FROM tableName
GROUP BY Name
ORDER BY COUNT(name) DESC

or you can just add ALIAS on it

SELECT Name, COUNT(name) totalCount
FROM tableName
GROUP BY Name
ORDER BY totalCount DESC
Sign up to request clarification or add additional context in comments.

5 Comments

Thats perfect except I need it to be sorted by the largest number of names first :)
@user1650499 do you mean name that has many count? if so, change it to ORDER BY COUNT(NAME) DESC
@user1650499 you can also add alias on it, see the updated answer above :)
@user1650499 don't forget to accept answers if it really helped you a lot :)
+1 for adding s to the end of need ... and also being correct :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.