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
EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
7369 | SMITH | CLERK | 7902 | 12/17/1980 12:00:00 AM | 800.00 | 20 | |
7499 | ALLEN | SALESMAN | 7698 | 2/20/1981 12:00:00 AM | 1600.00 | 300.00 | 30 |
7521 | WARD | SALESMAN | 7698 | 2/22/1981 12:00:00 AM | 1250.00 | 500.00 | 30 |
7566 | JONES | MANAGER | 7839 | 4/2/1981 12:00:00 AM | 2975.00 | 300.00 | 20 |
7654 | MARTIN | SALESMAN | 7698 | 9/28/1981 12:00:00 AM | 1250.00 | 1400.00 | 30 |
7698 | BLAKE | MANAGER | 7839 | 5/1/1981 12:00:00 AM | 2850.00 | 30 | |
7782 | CLARK | MANAGER | 7839 | 6/9/1981 12:00:00 AM | 2450.00 | 10 | |
7839 | KING | PRESIDENT | 11/17/1981 12:00:00 AM | 5000.00 | 10 | ||
7844 | TURNER | SALESMAN | 7698 | 9/8/1981 12:00:00 AM | 1500.00 | 0.00 | 30 |
7900 | JAMES | CLERK | 7698 | 12/3/1981 12:00:00 AM | 950.00 | 30 | |
7902 | FORD | ANALYST | 7566 | 12/3/1981 12:00:00 AM | 3000.00 | 20 | |
7934 | MILLER | CLERK | 7782 | 1/23/1982 12:00:00 AM | 1300.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
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
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