Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Count by range

How can I select the number of population by age group

count ( 0->10)
count ( 11->20)
like image 803
Chaabelasri E. Avatar asked Dec 06 '25 08:12

Chaabelasri E.


2 Answers

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.

like image 122
Borja Avatar answered Dec 07 '25 22:12

Borja


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.

like image 33
eumiro Avatar answered Dec 07 '25 21:12

eumiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!