Skip to content

The COUNT() function returns the number of records returned by a SELECT query.

func.count(<column>)
func.count(table.grade).alias('count_valid_grades')
| count_valid_grades |
|--------------------|
| 4 |
COUNT(<expr>)
ArgumentsDescription
<expr>Any expression.
This may be a column name, the result of another function, or a math operation.
* is also allowed, to indicate pure row counting.

An integer.

Create a Table and Insert Sample Data

CREATE TABLE students (
id INT,
name VARCHAR,
age INT,
grade FLOAT NULL
);
INSERT INTO students (id, name, age, grade)
VALUES (1, 'John', 21, 85),
(2, 'Emma', 22, NULL),
(3, 'Alice', 23, 90),
(4, 'Michael', 21, 88),
(5, 'Sophie', 22, 92);

Query Demo: Count Students with Valid Grades

SELECT COUNT(grade) AS count_valid_grades
FROM students;

Result

| count_valid_grades |
|--------------------|
| 4 |