SQL Functions : COUNT(), AVG() and SUM()

In This Article, You Will Know About SQL Functions. 

SQL Functions – Before moving ahead, let’s know a bit about SQL MIN() and MAX() Functions.

Table of Contents

SQL COUNT() Function

The COUNT() function returns the number of records after matching the specified condition.

Syntax

				
					SELECT COUNT(column_name)
FROM table_name
WHERE condition;
				
			

SQL AVG() Function

The AVG() function returns the average value of a numerical value’s column.

Syntax

				
					SELECT AVG(column_name)
FROM table_name
WHERE condition;
				
			

SQL SUM() Function

The SUM() function returns the total value of a selected numerical value’s column.

Syntax

				
					SELECT SUM(column_name)
FROM table_name
WHERE condition;
				
			

Example Database

Below is the example of a database table named “details.”

Id

Name

Class

section_id

1

Alex

5

1

2

Rolex

6

2

3

Salex

7

2

4

Lelex

8

3

5

Kelex

9

5

SQL COUNT()

Example: Use COUNT() function to return the total number of ‘Class’.

				
					SELECT COUNT(class)
FROM details;
				
			

Example: Use COUNT() function to return the total number of ‘Class’ for a specified condition.

				
					SELECT COUNT(class)
FROM details
WHERE section_id = 2;
				
			

SQL AVG()

Example: Use AVG() function to return the average value of ‘Class’.

				
					SELECT AVG(class)
FROM details;
				
			

As a result, it returns the average value of ‘Class’ column.

Example: Use AVG() function to return the average value of ‘Class’.

				
					SELECT AVG(class)
FROM details
WHERE section_id = 2;
				
			

As a result, it returns the average value of ‘Class’ column with a specified condition.

SQL SUM()

Example: Use SUM() function to return the SUM value of ‘Class’.

				
					SELECT SUM(class)
FROM details;
				
			

As a result, it returns the sum of ‘Class’ column.

Example: Use SUM() function to return the SUM value of ‘Class’.

				
					SELECT SUM(class)
FROM details
WHERE section_id = 2;
				
			

As a result, it returns the SUM value of ‘Class’ column with a specified condition.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Leave a Comment

Stay in the loop

codingstreets