Alex Chapp Alex Chapp - 24 days ago 17
MySQL Question

mySQL count only returning one result unless using group by

using the SQL query

select u.name,count(u.name) as 'followers'
from user u,follow f
where u.type = 'c' AND f.followee = u.email
group by u.name


gets me the correct value for all users in my database, however, the exact same query without the group by line only gives me the first value. I am learning SQL for the first time and was having a hard time figuring out why this is.

Answer

When you use count without group by it will count all the records and returns single line while when you use count with group by it will group the users on the base of their names and returns the count of each group.

Comments