Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

COUNT() Function

Learn how to count rows and values in SQL Server using the COUNT() aggregate function.

What is COUNT()?

The COUNT() function is an aggregate function in SQL Server used to count rows or values from a table.

It is commonly used when we want to know how many records are present in a table or how many records satisfy a particular condition.

In simple words:

COUNT() tells us how many records are available.

COUNT() Syntax

SELECT COUNT(column_name)
FROM table_name;

To count all rows, we commonly use:

SELECT COUNT(*)
FROM table_name;

Example Table

Consider the following Employees table:

EmployeeId EmployeeName Department Salary
1 Rahul IT 50000
2 Priya HR 40000
3 Amit IT 60000
4 Neha HR 45000
5 Rohit Sales 35000

COUNT(*)

COUNT(*) counts all rows in the table.

SELECT COUNT(*) AS TotalEmployees
FROM Employees;

Output

TotalEmployees
5

The result is 5 because the Employees table contains five rows.

COUNT(ColumnName)

We can also specify a particular column inside COUNT(). In this case, SQL Server counts the non-NULL values in that column.

SELECT COUNT(Salary) AS SalaryCount
FROM Employees;

If all Salary values are available, the result will be:

SalaryCount
-----------
5

COUNT() with WHERE

COUNT() can be combined with WHERE to count only the records that satisfy a condition.

For example, count employees whose salary is greater than 40000:

SELECT COUNT(*) AS EmployeeCount
FROM Employees
WHERE Salary > 40000;

Output

EmployeeCount
-------------
3

COUNT() with GROUP BY

COUNT() can be used with GROUP BY to count records separately for each group.

SELECT Department,
       COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department;

Output

Department EmployeeCount
HR 2
IT 2
Sales 1

COUNT(DISTINCT)

COUNT(DISTINCT column) is used to count unique non-NULL values.

For example, to count the number of different departments:

SELECT COUNT(DISTINCT Department)
       AS TotalDepartments
FROM Employees;

Output

TotalDepartments
----------------
3

The three unique departments are IT, HR and Sales.

COUNT(*) vs COUNT(Column)

COUNT(*) COUNT(Column)
Counts all rows. Counts non-NULL values.
NULL values do not affect row count. NULL values are not counted.
Useful for total records. Useful for counting available values.

Important Points

  • COUNT() is an aggregate function.
  • COUNT(*) counts all rows.
  • COUNT(column) counts non-NULL values.
  • COUNT() can be used with WHERE.
  • COUNT() can be used with GROUP BY.
  • COUNT(DISTINCT column) counts unique values.

Summary

COUNT() is used to count rows or non-NULL values in SQL Server. COUNT(*) counts all rows, while COUNT(column) counts only non-NULL values. COUNT() can also be combined with WHERE, GROUP BY and DISTINCT to perform different types of counting operations.