SQL (Function) : Types of Functions
SQL (Function) : Types of Functions
SQL (Function) : Types of Functions
It is an set of SQL statements that expect only input parameters, that perform
action and return the result.
A function can return an only single value or a table. Function are used as a part
of SQL commands.
Types of functions:
1. Scalar function
2. Aggregate function
Scarlar function:
A scalar function is a function that operates on the scalar values. It takes one or
more inputs values as arguments directly and return a value.
Math function.
Text function.
Date function.
Etc.
These function can be applied over the columns of a table to perform relevant
operation on the value of each record.
Aggregate function:
Min()
Max()
AVG()
Sum()
Count()
Math function:
Math function or the mathematical function performs the operation over the
different numeric values.
Power()
Round()
Mod()
Power():
Power function returns the value of number raised to the power of another
number. The power function is also called POW().
>> 125
>> 256
Round ():
>> 454.35
>> 454
>> 500
>> 454.31
Mod():
>> 1
>> 1.5
Text function:
the text function is performed over the test values.
Syntax: UPPER(str)
>> INFORMATION_PRACTICES
Syntax: lower(str)
>> mohit
#Substring()
Substring performs the operations over the string values. The forms without
len(length) argument return a substring from a string(str).
Starting at the position pos(). The forms with a len argument returns a substring
len character long from string(str).
>> gibility
Length(str):
In text length function performs operations over the string values. Length
function returns the length of str.
Syntax: length(‘str’)
>> 4
Eg:2:- mysql> select length(‘information_practices’);
>> 21
>> 5
Left function: Left function returns the left most len character from the string
value Or null if any argument is null.
>> eligi
>> informatics
Right function:
The right function returns the right most character from the string(str) or null if
any argument is null.
>> bility
Instring function returns the position of the first occurance of the substring in
string(str).
>> 6
>> 5
Ltrim(str):
Ltrim() returns the string with leading space. Ltrim removes the space characters.
>> toolbox
Rtrim(str):
Rtrim function return the string(str) with trailing space characters remove.
>> toolbox
Trim(str):
Trim function returns the string with all space character prefix removed.
>> toolbar
>> toolbox
Date function:
In sql date function performs operations over the date values. Following are the
different types of date().
1. NOW()
2. DATE()
3. MONTH()
4. MONTHNAME()
5. YEAR()
6. DAY()
7. DAYNAME()
NOW()
Now() returns the current date and time at a value in ‘yyyy-mm-dd hh:mm:ss’
format depends on their location. The function is used in string or numeric
content.
DATE(): Date function extract the date part from the date expression (expr).
>> 2023-11-17
MONTH()
Month function returns the month for date in the range of 1 to 12.
>> 11
MONTHNAME()
>> November
Year()
Year function returns the year of the date in the range of 1000 to 9999.
>> 2023
DAY()
Day function return the day of the month in the range of 1 to 31.
Eg: mysql> select day(‘2023-11-17’);
>> 17
DAYNAME()
Dayname function returns the week day name from the date.
>> Friday
Aggregate()
Sql performs operation over the set of values or apply on the coloumn aggregate
function are also called group function.
MAX()
MIN()
AVG()
SUM()
COUNT()
Employee(Emp)
Output:
>> 5000
>> 3500
>> 4200
>> 21000
>> 5
>> 7
MAX():- max function used to get the maximum value from a column to get the
maximum salary(as per the given table) draw by employees the query would be:-
MIN():- min function is used to get the minimum value from a column. To get the
minimum salary drawn by an employee, the query would be:-
AVG():- avg function is used to get the average values of a numeric column. To get
the average salary drawn by an employee the query would be:-
COUNT():- count function read the numbers of row in the given table that satisfy
the condition in WHERE clause if the WHERE clause condition is not satisfied then
the query returns the total numbers of rows in the given table.
The output will be 7 (as per the given Emp table). Another condition of count
function.
>> 3
Mysql order by clause to sort the table data in accending and decending order by
default data is not insert into the table in any order than the data will be arranged
in accending order.
If we want to return the value in any particular order then we have to sort it by
using my sql order by clause or statement.
Syntax:-
Mystudent
Roll no. Student name Class Marks
001 Ayush 9 95
002 Mohit 12 70
003 Tanya 11 85
004 Subh 11 95
005 Mansi 9 63
006 Krishna 12 84
007 Umang 10 46
Mysql> select* FROM my student
GROUP BY clause
The GROUP BY clause groups a set of rows or records into a set of summay
row/record by by value of column. It return one row for each group.
>>
Class
9
10
11
12
Eg: mysql> select * class FROM mystudent;
>>
>>
Class Count
9 2
10 1
11 2
12 2
>>
>>
Eg: mysql> select class, AVG(marks) FROM mystudent WHERE class > 10GROUP
BY class order by marks(desc);
>>
Or
The having clause is used with the group of clause to filter group base on a
specified condition. WHERE is applied before GROUP BY , HAVING is applied after
filter or aggregates.
Eg: mysql> select class, avg FROM mystudent GROUP BY class having avg marks <
90;
>>
>>