Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL - Getting Started
  • What is SQL
  • Create Table
  • ALTER TABLE Statements
  • Rename Columns
  • Modify Column Type
  • Drop Columns
  • Rename Tables
  • Drop Tables
  • Insert Statement
  • Update Statement
  • Delete Statement
  • Truncate Statement
  • Merge Statement
  • Null Value
  • Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL - Inner Join
  • SQL - Left Join
  • SQL - Right Join
  • SQL - Full Join
  • SQL - BETWEEN
  • SQL - IN
  • SQL - LIKE
  • SQL - INTERSECT
  • SQL - MINUS
  • SQL - UNION
  • SQL - UNION ALL
  • SQL - DISTINCT
  • SQL - ANY, SOME
  • SQL - ALL
  • SQL - AVG()
  • SQL - COUNT()
  • SQL - MAX()
  • SQL - MIN()
  • SQL - SUM()
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL - COUNT() Function

The COUNT() function is an aggregate function that is used to find the number of values in the specified column excluding NULL values. It can be applied on numeric, character, or date values.

Syntax:

SELECT COUNT([ALL | DISTINCT] expression | column_name) FROM table_name [WHERE condition] [GROUP BY]    ;

For the demo purpose, we will use the following Employee table.

EmpIdFirstNameLastNameEmailSalaryDeptId
1JohnKing'john.king@abc.com'2400010
2JamesBond1700020
3NeenaKochhar'neena@test.com'1500020
4LexKing'lex@test.com'900030
5AmitPatel6000030
6AbdulKalam'abdul@test.com'480040

Specify the column name to count the total values in that column excluding NULL values. The following counts the total records in the FirstName column.

SQL Script: COUNT()
SELECT COUNT(FirstName) AS "Total" FROM Employee;
Total
6

The COUNT() function excludes the NULL values.

SQL Script: COUNT()
SELECT COUNT(Email) AS "Total" FROM Employee;
Total
4

The COUNT() function can contain other functions such as Distinct, as shown below.

SQL Script: COUNT()
SELECT COUNT(Distinct(LastName)) AS "Total" FROM Employee;
Total
5

Use the COUNT(*) to count the total records in the table. The following query count the total records of the Employee table.

SQL Script: COUNT(*)
SELECT COUNT(*) AS "Total Records" FROM Employee;
Total Records
6

The COUNT() is an aggregate function, so it can be used in Group By queries. The following query counts the number of employees in each department.

SQL Script: COUNT() with Group By
SELECT DeptId, COUNT(*) AS "Total Employees" FROM Employee GROUP BY DeptId;
DeptIdTotal Employees
101
202
302
401
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

contact@tutorialsteacher.com

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.