0

Lets say I have a table with a column of ages..

Here is the list of ages

  • 1
  • 2
  • 3
  • 1
  • 1
  • 3

I want the SQL to count how many of age 1s, how many of 2s and 3s.

The code:

Select count(age) as age1 where age = ‘1’;
Select count(age) as age2 where age = ‘2’;
Select count(age) as age3 where age = ‘3’;

Should work but would there be a way to just display it all using only 1 line of code?

1
  • I may be biased, but using GROUP BY seems to be the most elegant way to solve the problem. This is especially true as your table grows and contain additional age values. Commented Mar 17, 2014 at 21:05

6 Answers 6

1

This is an instance where the GROUP BY clause really shines:

SELECT age, COUNT(age)
FROM table_name
GROUP BY age

Just an additional tip:

You shouldn't use single quotes here in your query:

WHERE age = '1';

This is because age is an INT data type and therefore does not have single quotes. MySQL will implicitly convert age to the correct data type for you - and it's a negligible amount of overhead here. But imagine if you were doing a JOIN of two tables with millions of rows, then the overhead introduced would be something to consider.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this ,if the count is limited to three ages ,also using aggregate functions without grouping them will result in a single row,you can use SUM() with the condition which will result in a boolean and you can get the count based on your criteria

Select SUM(age = '1') as age1,
SUM(age = '2') as age2,
SUM(age = '3') as age3
from table

Comments

0
SELECT SUM(CASE WHEN age = 1 THEN 1 ELSE 0 END) AS age1,
       SUM(CASE WHEN age = 2 THEN 1 ELSE 0 END) AS age2,
       SUM(CASE WHEN age = 3 THEN 1 ELSE 0 END) AS age3
FROM YourTable

Comments

0

If your query should return only one column (age in this case, you can use Count+groupby):

SELECT age, Count(1) as qty
FROM [yourTable]
GROUP BY age

Remember you must include any additional column in your group by condition.

Comments

0
Select age as Age_Group, count(age) as Total_count from table1 group by age;

Comments

0
select age, count(age) from SomeTable group by age

http://sqlfiddle.com/#!2/b40da/2

The group by clause works like this:

When using aggregate functions, like the count function without a group by clause the function will apply to the entire dataset determined by the from and where clauses. A count will for instance count the number of rows in the result set, and sum over a specfic column will sum all the rows in the result set.

What the group by clause allows us to do, is to divide the result set determined by the from and where clause into partitions, so that the aggregate functions no longer applies to the result set as a whole, but rather within each partition of the result set.

When you specify a column to group by, what you are saying is something like "for each distinct value of column x in the result set, create a partition containing any row in the result set with this particular value in column x". Then, instead of yielding one result covering the entire resultset, aggregate functions will yield one result for each distinct value of column x in the result set.

With your example input of:

1
2
3
1
1
3

let's analyze the above query. As always, we should look at the from clause and the where clause first. The from clause tells us that we are selecting from SomeTable and only this, and the lack of a where clause tells us that we are selecting from the full contents of SomeTable.

Next, we'll look at the group by clause. It's present, and it groups by the age column, which is the only column in our example. The presence of the group by clause changes our dataset completely! Instead of selecting from the entire row set of SomeTable, we are now selecting from a set of partitions, one for each distinct value of the age-column in our original result set (which was every row in SomeTable).

At last, we'll look at the select-clause. Now, since we are selecting from partitions and not regular rows, the select-clause has fewer options for what it can contain, actually it only has 2: The column that it is grouped by, or an aggregate function.

Now, in our example we only have one column, but consider that we had another column, like here:

http://sqlfiddle.com/#!2/d5479/2

Now, imagine that in our data set we have two rows, both with age='1', but with different values in the other column. If we were to include this other column in a query that is grouped by the age-column (which we now know will return one row for each partition over the age-column), which value should be presented in the result? It makes no sense to include other column than the one you grouped by. (I'll leave multiple columns in the group by clause out of this, in my experience one usually just wants one..)

But back to our select-clause, knowing our dataset has the distinct values {1, 2, 3} in the age-column, we should expect to get 3 rows in our result set. The first thing to be selected is the age-column, which will yield the values [1, 2, 3]´ in the three rows. Next in theselect-list is an aggregate functioncount(age), which we now know will count the number of rows in each partition. So, for the row in the result whereage='1', it will count the number of rows withage='1', for the row whereage='2'it will count the number of rows whereage='2'`, and so on.

The result would look something like this:

age    count(age)
 1        3
 2        1
 3        2

(of course you are free to override the name of the second column in the result, with the as-operator..)

And that concludes today's lesson.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.