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.
How can I count distinct records in SQL?
Syntax. SELECT COUNT(DISTINCT column) FROM table; This statement would count all the unique entries of the attribute column in the table . DISTINCT ensures that repeated entries are only counted once.
Can we use distinct with Count in SQL?
Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table. ... If you do not use DISTINCT, then COUNT() function gives the count of all rows.