0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO
Rishabh Gupta
2 weeks ago
Rishabhcountry asked

Why can’t we select customer names when using GROUP BY to calculate average age by country?

Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Rishabh, really nice question.

When you use GROUP BY, you’re asking SQL to take many rows and squeeze them into one row per group. In your case, the groups are countries. That means all customers from the same country get combined so you can calculate something about the whole group — like the average age.

Here’s the key idea:
A grouped result needs one clear value per group.
The country has one value, the average age has one value… but the name does not. There are many customer names in each country, so SQL doesn’t know which one to show.

That’s why this works:

SELECT country, AVG(age) AS average_age
FROM Customers
GROUP BY country;

But adding a name does not make sense unless you also group by it or aggregate it. If you tried:

SELECT country, name, AVG(age)

SQL would ask: “Which name should I show for this country? There are many.”

Once you see it that way, it becomes clear:
GROUP BY is for summaries, not individual details.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.