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

0 votes
46 views
Ethan Karla asked 7 days ago in Database by Ethan Karla

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

1 Answer

0 votes
Ashutosh Kumar Verma answered 6 days ago by Ashutosh Kumar Verma

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.