Count Records in SQL Group By

How to Count Records in SQL Group with an example? For this, We are going to use the [MyEmployee table] table present in our Database. From the below figure, you can observe that the [MyEmployee table] table has fourteen records.

Employee Table 1

How to Count Records in SQL Group By?

Generally, we will write queries to check for the total number of products that belong to a particular category or color, etc. In these situations, we use the SQL Sever GROUP BY clause to group the products by category or color and count records.

To find the total number of records present in each group, we have to use the COUNT function. Let us see the SQL Server Example.

SELECT COUNT( [EmployeeID]) AS [Total Records]
      ,[Education]
      ,SUM([YearlyIncome]) AS Income
      ,SUM([Sales]) AS Sale
 FROM [MyEmployees Table]
 GROUP BY [Education]
 ORDER BY Income DESC
Count Records in SQL Server Group by 2

TIP: The Group By Clause will also combine the NULL records, and count them as well. You can remove the NULL values by using the WHERE Clause with NOT NULL.

Categories SQL