Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting occurrences of unique values in a column using sql

Tags:

sql

Is there a simple way for counting occurrences of unique values in a column using sql.

for e.g if my column is

a
a
b
a
b
c
d
d
a

Then the output should be

a 4
b 2
c 1
d 2

like image 200
r15habh Avatar asked Oct 19 '11 09:10

r15habh


People also ask

How do I count unique rows in SQL?

The COUNT(DISTINCT) function returns the number of rows with unique non-NULL values. Hence, the inclusion of the DISTINCT keyword eliminates duplicate rows from the count. Its syntax is: COUNT(DISTINCT expr,[expr...])

How do I count multiple values in one column in SQL?

You can count multiple COUNT() for multiple conditions in a single query using GROUP BY. SELECT yourColumnName,COUNT(*) from yourTableName group by yourColumnName; To understand the above syntax, let us first create a table.

How do I extract unique distinct values from a column in SQL?

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.


2 Answers

SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
like image 180
sll Avatar answered Sep 19 '22 21:09

sll


Use GROUP BY and COUNT

SELECT column, COUNT(*)
FROM table
GROUP BY column
like image 34
Konerak Avatar answered Sep 17 '22 21:09

Konerak