What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

Asked 14-Sep-2021
Updated 12-Apr-2023
Viewed 1182 times

0

What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example. 


2 Answers


0

Count all the DISTINCT program names by program type and push number

SELECT COUNT(DISTINCT program_name) AS Count,
  program_type AS [Type]
FROM cm_production
WHERE push_number = @push_number
GROUP BY program_type

DISTINCT COUNT(*) will return a row for each unique count. What you want is COUNT(DISTINCT <expression>): evaluates expression for each row in a group and returns the number of unique, non-null values.


0

Function in SQL Server-

     A function is a set of code that perform specific task. There are some predefined function in SQL Server are as follow-

COUNT(), AVG(), SUM(), MAX(), MIN().

SQL COUNT(), AVG() and SUM() functions –

COUNT() –

The SQL COUNT () function is used to find the number of rows in a column of a table.

Syntax-

                        SELECT COUNT(column_ name) FROM table_name WHERE condition;

AVG() function-

The SQL AVG() function is used to find the Average of numerical column values of a table.

Syntax-

                        SELECT AVG(column_name) FROM table_name WHERE condition;

SUM() function –

 The SQL SUM() function is used to find the sum of numerical column values in a table.

Syntax-

                        SELECT SUM(column_name) FROM table_name WHERE condition;

            We have a database table “TeacherDetails”-

                                What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

Example-

COUNT()-     The following statement is to find the number of row in a column of ‘TeacherDetails’ table.

                                      select count(stuCourse) from TeacherData;

                                        What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

AVG()-        The following statement is to find the Average of numerical column values of ‘TeacherDetails’ table.

                                           select avg(Salary) from TeacherData;

                                            What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

SUM()-     The following statement is to find the sum of the numerical column values in table ‘TeacherDetails’.

                                       select sum(Salary) from TeacherData;

                                                What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.


  SQL MIN and MAX –

The SQL function MIN() and MAX() is to find the minimum and maximum values form database table.

The MIN() function is used to find the minimum value from table.

The MAX() function is used to find the maximum value from table.

MIN( )      Syntax-

                            SELECT MIN(column_name) FROM table_name WHERE condition;

MAX() Syntax-

                             SELECT MAX(column_name) FROM table_name WHERE condition; MAX() function

Example-

-                   Let have a table ‘TeacherDetails’.

                                                What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

MAX()-    Follow the statement to find the maximum value from ‘TeacherDetails’ table.

                        select max(Salary) from TeacherData;

                                                          What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

To find the record which has maximum Salary then you can use the following statement.

                    select * from TeacherData where Salary=(select max(Salary) from TeacherData);

                                                        What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

MIN()-        The following statement is used to find the minimum Salary from ‘TeacherDetails’ table.

                               select min(Salary) from TeacherData;

                                                        What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.

To find the record of that row which has minimum value is like as-

                   select * from TeacherData where Salary =(select min(Salary) from TeacherData);

                                                            What is function in Database? explain some function like Sum(), Count(), Max(), Min(), AVG() with example.