What is the difference between COUNT(*) and COUNT(ColName)?

Question

Explain with any example code

solved 1
TheDataMonk 55 years 14 Answers 2167 views Grand Master 0

Answers ( 14 )

  1. COUNT(*) : It will return total number of records in table.
    COUNT(ColName) : It will return total number of records where value for that ColName is Not-Null.

    Eg: Table A
    ID, Name, Dept
    1,’A’,’D1′
    2,’B’,NULL
    3,’C’,’D5′

    COUNT(*) : 3
    COUNT(Dept) : 2
    COUNT(ID) : 3

    Best answer
    2

    when you use count(*) it will give count for all the rows in a table
    when you use count(colname), it will give count of all the rows where colName is not Null

  2. count(*) – Returns the count of the total record of the table
    count(column_name) – Returns the total count of non-null values in the column

  3. count(*) will return the count of the total record of the table
    count(column_name) will return the total count of non-null values in the column

  4. COUNT(*) -Returns the count of total records present in the table
    COUNT(column_name) Returns the total count of non_null values present in the column.

  5. COUNT(*) : gives the total number of record in the table
    COUNT(colname) : gives the total count of the particular column which is passed in COUNT function and it only gives count of NON-NULL values.

    for example:

    table student
    columns : stuID, stuNAME, stuSUBJECT

    01,CHETAN,’MATHS’
    02,’DIKSHA’,’ENGLISH’
    03,’SIDD’,’SCIENCE’
    04,’RUCHITA’,NULL

    COUNT(*) = 4
    COUNT(stuSUBJECT) = 3

  6. The count(*) returns all rows whether column contains null value or not while count(columnName) returns the number of rows except null rows.

  7. COUNT(*) counts all rows
    COUNT(column) counts non-NULLs only
    Another minor difference between using * and a specific column is that in case of column you can add the keyword DISTINCT and restrict the count to distinct values.

    Note: COUNT(1) is the same as COUNT(*) because 1 is a non-null expression

  8. In the SQL query, “COUNT” is the most used function. It is used for taking the count of the records.
    We can pass various parameters in the count function. It’s output changes as per the parameters passed in it.

    COUNT(*) – Returns the total number of records in a table (Including NULL valued records).

    COUNT(Column Name) – Returns the total number of Non-NULL records. It means that it ignores counting NULL valued records in that particular column.

  9. COUNT(*) will return total number of records in table.
    COUNT(ColName) will return total number of records where value for that ColName is Not-Null.

    Table a
    ID, work, add
    1,’c’,’D′
    2,’x’,NULL
    3,’z’,’E′

    COUNT(*) : 3
    COUNT(add) : 2
    COUNT(ID) : 3

  10. COUNT(*) – Returns the total number of records in a table (Including NULL values).

    COUNT(Column Name) – Returns the number of Non-NULL records of that particular column. It ignores counting NULL valued records in that particular column.

  11. COUNT(*) returns total number of records in a table.
    COUNT(ColName) returns total number of records where value of ColName is Not Null.

    e.g.

    Table Employee
    Id, Name, Exp
    1,’a’,2
    2,’b’,NULL
    3,’c’,4

    COUNT(*) : 3
    COUNT(Id) : 3
    COUNT(Exp) : 2

  12. COUNT(*) return the total number of records in a table.
    COUNT(col_name) returns the total number of records where value of col_name is NOT NULL.

  13. Count(*) returns total number of records from whole table.
    Count(column_name) returns total number of records from that specified column_name within the table.

Leave an answer

Browse
Browse