tee
tee

Reputation: 1336

sql how to get a count based on two columns

I'm pretty new to sql and I'm just wondering how I can get a count based on the values of two columns. Here's an example:

Sex |  age

 M  |  7

 M  |  5

 F  |  5 

 M  |  7

The resulting table would have an extra column that is the count of how many M or F are a certain age. So in this case it would be this:

Sex | age  | Count

 M  |  7   |  2

 M  |  5   |  1

 F  |  5   |  1

Any help would be great. Thanks!

Upvotes: 2

Views: 8657

Answers (2)

pshirishreddy
pshirishreddy

Reputation: 746

select count(*), sex, age from table group by sex, age;

Upvotes: 1

SqlZim
SqlZim

Reputation: 38023

This is a basic aggregation query. using group by:

select sex, age, count(*) as Cnt
from t
group by sex, age

Upvotes: 8

Related Questions