How do I write an SQL query to count the total number of a specific num value in the num column of a table?
Assuming we have the following data.
NAME | NUM |
---|---|
SAM | 1 |
BOB | 1 |
JAKE | 2 |
JOHN | 4 |
Take the following query:
SELECT WHERE num = 1;
This would return these two rows.
NAME | NUM |
---|---|
SAM | 1 |
BOB | 1 |
Tip: If you want to count the duplicates in the whole Column, use this formula =COUNTIF(A:A, A2) (the Column A indicates column of data, and A2 stands the cell you want to count the frequency, you can change them as you need).
Try
SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME
If you want to have the result for all values of NUM
:
SELECT `NUM`, COUNT(*) AS `count` FROM yourTable GROUP BY `NUM`
Or just for one specific:
SELECT `NUM`, COUNT(*) AS `count` FROM yourTable WHERE `NUM`=1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With