Chaabelasri E.
Chaabelasri E.

Reputation: 170

SQL - Count by range

How can I select the number of population by age group

count ( 0->10)
count ( 11->20)

Upvotes: 3

Views: 2605

Answers (2)

eumiro
eumiro

Reputation: 213125

Try this:

SELECT FLOOR(age / 10), COUNT(*)
FROM yourTable
GROUP BY FLOOR(age / 10)

Manipulate the age / 10 expression to get the exact ranges. This will return 0 for ages 0-9, 1 for ages 10-19, etc.

Upvotes: 4

Borja
Borja

Reputation: 2198

There are other question about the same, you can found the solution on: In SQL, how can you "group by" in ranges?

The syntax is valid for mysql too.

Upvotes: 5

Related Questions