SQL COUNT
The COUNT() function is an aggregate function used to count the number of rows or values in a table.
👉 In simple words:
COUNT() returns the total number of records that match a condition.
Important Points
1. COUNT(*) → counts all rows
2. COUNT(column) → counts non-NULL values
3. COUNT(DISTINCT column) → counts unique values
4. Often used with GROUP BY for reports
Basic Syntax
SELECT COUNT(column_name) FROM table_name; Explanation COUNT(column_name) → Counts non-NULL values in a column COUNT(*) → Counts all rows in the table
COUNT All Rows
SELECT COUNT(*) FROM Students; This counts all rows, including rows with NULL values.
COUNT Specific Column
SELECT COUNT(Age) FROM Students; COUNT(column) does not count NULL values
COUNT with WHERE
SELECT COUNT(*) FROM Students WHERE Class = 10;
COUNT with GROUP BY
SELECT Class, COUNT(*) AS TotalStudents FROM Students GROUP BY Class;
COUNT with DISTINCT
SELECT COUNT(DISTINCT Class) FROM Students;
Simple Definition (Exam/Interview):
COUNT() is an SQL aggregate function used to count the number of rows or non-NULL values in a table or column.