Friday, February 15, 2013

SQL Server COUNT function


SQL Server COUNT function

 SQL Server COUNT function returns number of items in a group.

COUNT(*) -> returns number of rows in the table
COUNT(col) -> returns non-null values count
COUNT(distinct col)-> returns non-null unique values from the column.

Count(Sal) on Emp table


EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7369SMITHCLERK790212/17/1980 12:00:00 AM800.00
20
7499ALLENSALESMAN76982/20/1981 12:00:00 AM1600.00300.0030
7521WARDSALESMAN76982/22/1981 12:00:00 AM1250.00500.0030
7566JONESMANAGER78394/2/1981 12:00:00 AM2975.00300.0020
7654MARTINSALESMAN76989/28/1981 12:00:00 AM1250.001400.0030
7698BLAKEMANAGER78395/1/1981 12:00:00 AM2850.00
30
7782CLARKMANAGER78396/9/1981 12:00:00 AM2450.00
10
7839KINGPRESIDENT
11/17/1981 12:00:00 AM5000.00
10
7844TURNERSALESMAN76989/8/1981 12:00:00 AM1500.000.0030
7900JAMESCLERK769812/3/1981 12:00:00 AM950.00
30
7902FORDANALYST756612/3/1981 12:00:00 AM3000.00
20
7934MILLERCLERK77821/23/1982 12:00:00 AM1300.00
10

Query:  Select number of records in the table emp
    
             select count(*) from emp;
O/P:
         12

         select count(*) as "Number of Records"  from emp;

O/P:       
            "Number of Records"
             800.00

Query: Select employee count of Dept No 10:

select count(*)  from emp where deptno=10;

O/P: 3


Query: Get  number of comm entries in the emp table

select count(comm)  from emp

O/P :5
 
select count(distinct comm)  from emp
O/P :4
 
 
Note: count(columnname) returns non-null  values.  count(distinct columnname) returns number of non-null unique values.




Tags:SQL-Server COUNT function, SQL Server COUNT function, SQL Server Aggregate function, SQL Server 2012 COUNT function,using COUNT in T-SQL,using COUNT in Select,using COUNT in Having,COUNT in T-SQL

No comments:

Post a Comment