Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count occurrences of a column value efficiently in SQL?

I have a table of students:

id | age -------- 0  | 25 1  | 25 2  | 23 

I want to query for all students, and an additional column that counts how many students are of the same age:

id | age | count ---------------- 0  | 25  | 2 1  | 25  | 2 2  | 23  | 1 

What's the most efficient way of doing this? I fear that a sub-query will be slow, and I'm wondering if there's a better way. Is there?

like image 737
Assaf Lavie Avatar asked Oct 01 '09 13:10

Assaf Lavie


People also ask

How do I count the number of instances in a column in SQL?

To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the COUNT function as DISTINCT . When given a column, COUNT returns the number of values in that column. Combining this with DISTINCT returns only the number of unique (and non-NULL) values.

Which is faster count (*) or count column?

The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values.

Why count 1 is faster than count (*)?

According to this theory, COUNT(*) takes all columns to count rows and COUNT(1) counts using the first column: Primary Key. Thanks to that, COUNT(1) is able to use index to count rows and it's much faster.

How do you make a count query faster?

So to make SELECT COUNT(*) queries fast, here's what to do:Get on any version that supports batch mode on columnstore indexes, and put a columnstore index on the table – although your experiences are going to vary dramatically depending on the kind of query you have.


1 Answers

This should work:

SELECT age, count(age)    FROM Students   GROUP by age 

If you need the id as well you could include the above as a sub query like so:

SELECT S.id, S.age, C.cnt   FROM Students  S        INNER JOIN (SELECT age, count(age) as cnt                      FROM Students                      GROUP BY age) C ON S.age = C.age 
like image 185
Mike Dinescu Avatar answered Oct 04 '22 13:10

Mike Dinescu