Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count how many rows have the same value [duplicate]

Tags:

sql

mysql

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
like image 837
user2273278 Avatar asked May 23 '13 07:05

user2273278


People also ask

How do I count repeated values in Excel?

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).


2 Answers

Try

SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME 

SQL FIDDLE

like image 97
Meherzad Avatar answered Oct 01 '22 09:10

Meherzad


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 
like image 30
Sirko Avatar answered Oct 01 '22 10:10

Sirko