SQL Count

The COUNT function in SQL is used to count the number of rows with a specific condition. It can count all rows in a table or subset of rows based on specific conditions.

SQL Count Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;



SQL Count Example

This query count all rows in the Patients table, including those with NULL values in any column.

SELECT COUNT(*)
FROM Patients;




SQL Count Specify Column Example

This query counts only the rows where the contact_number is not NULL in the Patients table.

SELECT COUNT(contact_number)
FROM Patients;




SQL Count Adding a WHERE Clause Example

This query counts the number of rows where the gender column has the value ‘F’, i.e., the number of female patients.

SELECT COUNT(*)
FROM Patients
WHERE gender = 'F';




SQL Count Ignore Duplicates Example

This query counts the number of unique values in the specialty column in the Doctors table, ignoring any dupicate specialties.

SELECT COUNT(DISTINCT specialty)
FROM Doctors;




SQL Count Using an Alias Example

This query counts all rows in the Patients table and uses an alias total_patients to give a meaningful name to the result.

SELECT COUNT(*) AS total_patients
FROM Patients;




SQL Count Visual Diagram




SQL Count Labs

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top