Open In App

MariaDB COUNT Functions

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MariaDB is an open-source and relational database to operates available data and displays the required value. the count, max, min, and other functions used to get particular information or count of the database data. the mariaDB count() function is used to get a COUNT of the row or available information row of the database. MariaDB is based on SQL and it supports the ACID style for data processing and ensuring atomicity, consistency, isolation, and durability for transactions. MariaDB offers built-in replication.

In this article, We will learn about the MariaDB COUNT() function and Its operation. we will see the use, syntax, Practical examples, and different features of the COUNT() function. After reading this article, You will able to perform any query with the COUNT() Function very easily.

COUNT Functions in MariaDB

The MariaDB COUNT() function is used to get a count of the given expression. We can use the COUNT() function with the column name and required expression. The MariaDB count function uses the single expression, distinct clause, and other database functions to get the required COUNT. The NULL values are not counted until they are explicitly included using the COUNT(*).

COUNT() can be combined with other aggregate functions like SUM(), AVG(), MIN(), and MAX() for various analysis tasks.

Syntax for COUNT Function:

SELECT COUNT(aggregate_expression)  
FROM table_name
[WHERE conditions];

Explanation:

  • The “select” keyword is used to display the COUNT of the expression.
  • The “COUNT” function is used to COUNT the rows of the input column’s information.
  • The “aggregate_expression” is used to get entire columns or single columns to COUNT values.
  • The “where condition” is used to apply the database condition with the clauses.

Note: The COUNT() Function counts the NOT NULL values of the expression.

Example of COUNT Functions

To understand the COUNT Function in detail we will need a table on which we perform various operations and queries. So here we have Student_tables which consist of st_id, st_name, st_address, and date_admission as Columns. After inserting some data into the table, The table looks:

studentTables

students Table

Example 1: Simple COUNT Functions

Let’s count the total number of student in the Student_Table.

Query:

SELECT COUNT(st_name) FROM Student_tables; 

Output:

SimpleCount

Output

Explanation:

  • The “st_name” column is used to count the values as an expression.
  • The column has eight rows with the not null information.

Example 2: MariaDB COUNT() Function with Single Expression

We have Given a “Student_tables” database, let’s determine the total number of students whose names are either “Ram” or “Jim.”

SELECT COUNT(*) AS "Number of Student Names"  
FROM Student_tables
WHERE st_name in ('Ram', 'Jim');

Output:

SingleExp

Output

Explanation:

  • The COUNT() function is used the “*” for entire columns of the table.
  • The condition applies with the “IN” clause with the “Ram” and “Jim” row.
  • The 2 rows shows the “Ram” name in the st_name and 1 row shows as the “Jim” name available in the table.
  • The query shows the output as an “3” counts.

Example 3: MariaDB COUNT() Function with Distinct Expression

Let’s Identify the unique count of students among only those named Ram or Jim in the Student_tables to avoid redundancy caused by duplicate names.

Query:

SELECT COUNT( DISTINCT st_name) AS "Unique Student's Names"  
FROM Student_tables
WHERE st_name in ('Ram', 'Jim');

Output:

DistinctExp

Output

Explanation:

  • The query shows the output as an “2” counts.
  • There are Ram name has duplicate entries but remove using distinct clause.
  • The 1 : the ram student name and 1 : the Jim name of the table.
  • The two Ram name has available in the St_name column but remove duplicate and COUNT only one.

Example 4: MariaDB COUNT() Function with Group By Clause

Let’s Identify the frequency of distinct admission dates for students in Student_tables. This helps to understand the enrollment trends by counting the number of students admitted on each day.

Query:

SELECT COUNT(date_admission) AS "Student date_admission"  
FROM Student_tables
group by
date_admission;

Output:

GroupBy

Output

Explanation:

  • We can use the “date_admission” as an aggregate expression of the mariaDB COUNT function.
  • The “group by” clause uses for the “date_admission” column of the student_tables table.
  • The output shows the five rows of the table with the “Student date_admission” column.
  • The output shows the three time two dates come and two unique date comes in the table.

Conclusion

The COUNT function is used in mariaDB function with the different conditions of the database. We can use the “where” and “distinct” clause to get the required COUNT value of the database. It helps to database developer to get total of the multiple values with the particular condition and information. We can see the available rows of the columns according to the data. the database COUNT function helps to operate and display multiple information and easy to handle the table.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads