The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Group By with count

Group By with count

0
Votes
1
Answer

On the Group by lesson, we have the query

Select first_name

from employees

group by first_name;

I match results, but when I add in COUNT after SELECT and before (first_name)

my returned results come back different with the same order of names or counts before the count function was added.

In other words, when adding the first_name before count(First_name) it comes back alphabetical, while mine comes out in order of Georgi, Bezalel, Parto etc.

 

Is this normal or okay, as long as the point of the query gets across, or must the results match exactly?

1 Answer

365 Team
0
Votes

Hi Alan!
Thanks for reaching out.
If I’ve managed to understand you correctly, you are referring to the following query, right?

SELECT 
first_name, COUNT(first_name)
FROM
employees
GROUP BY first_name;

If yes, then the output is not sorted in alphabetical order only because you have not added an ORDER BY clause in the following way.

SELECT 
first_name, COUNT(first_name)
FROM
employees
GROUP BY first_name
ORDER BY first_name;

Hope this helps.
Best,
Martin