COUNT function in MariaDB

MariaDB COUNT
To get the count of an expression, the MariaDB COUNT function is used.

Syntax:

SELECT expressions, 
COUNT (aggregate_expression)  
FROM table_name  
WHERE conditions;

Parameters:
Aggregate_expression: It is used to specify the column or expression to be utilised by the aggregate function.

Example 1: Using Group By Clause.
Players Table:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Dhoni	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Query:

SELECT sports, COUNT(*) AS “Number of Players”
FROM Players
GROUP BY sports;

Output:

SPORTS	         Number of Players
Cricket	         2
Football	 1
Badminton	 1
Boxing	         1

Explanation:
The ‘Players’ is an already existing table. Here we are counting the number of players in the same SPORTS group, using the Group By clause with an aggregate function COUNT.

Example 2: Using Distinct Clause.
Players Table:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Dhoni	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Query:

SELECT COUNT(DISTINCT sports) FROM Players;

Output:

4

Explanation:
The ‘Players’ is an already existing table. Here we are counting the number of unique values in the SPORTS column, using the Distinct clause with an aggregate function COUNT.

Example 3:
Players Table:

ID	NAME	      SPORTS
1	Sachin	      Cricket
2	Dhoni	      Cricket
3	Sunil	      Football
4	Srikanth	
5	Mary	      Boxing

Query:

SELECT COUNT(sports) FROM Players;

Output:

4

Explanation:
The count of the total number of NOT NULL values in the SPORTS column is 4 and so is the result.

Please Share