SQL Functions ONE SHOT - Lovejeet Arora 12IP
SQL Functions ONE SHOT - Lovejeet Arora 12IP
Lovejeet Arora
12th IP
A function is special types of command that performs some operation and returns a single
value as a result. It is similar to method or function in JAVA, which can be called by giving some
argument. MySQL offers following types of functions:
Numeric Functions – Functions which accepts/returns only numeric data type.
String Functions– Functions which accepts/returns only Text/Char data type.
Date & Time Function– Functions which accepts/returns only Date data type.
Aggregate Functions – Functions which works on whole column.
Numeric Functions
POWER() : Returns the argument raised to the specified power. POW () works the same way.
Example:
SELECT POW(2,4); Result:16
SELECT POW(2,-2); 2-2 = 1/22= ¼ = .25 Result:0.25
SELECT POW(-2,3); -23 = .-2* -2*-2 Result: –8
ROUND() : Rounds the argument to the zero decimal place, Whereas ROUND(X,d) rounds the
argument to d decimal places. Value will be Rounded-up when next number is more than 5
Example:
SELECT ROUND(-1.23); Result: -1
SELECT ROUND(-1.5); Result: 2
SELECT ROUND(-1.51); Result: 2
SELECT ROUND(3.798, 1); Result: 3.8
SELECT ROUND(1.298, 0); Result: 1
SELECT ROUND(323.298, -1); Result: 320
SELECT ROUND(323.598, -1); Result: 320
SELECT ROUND(325.598, -1); Result: 330
TRUNCATE() : Truncates the argument to specified number of decimal places.
Example:
SELECT TRUNCATE (7.29,1); Result: 7.2
SELECT TRUNCATE(27.29,-1); Result: 20
SIGN() : Returns sign of a given number.
Example :
SELECT SIGN (15); Result : 1
SELECT SIGN (-15); Result : -1
SELECT SIGN (0); Result : 0
MOD(m,n) : Returns the remainder after division .
Example :
SELECT MOD(25,7); Result : 4
SQRT() : Returns the square root of given number.
KVS REGIONAL OFFICE, JAIPUR 12 IP TERM-2 (2021-22) 1
MySQL Functions
Lovejeet Arora
12th IP
Example : SELECT SQRT (25); Result : 5
CHAR() : Returns the corresponding ASCII character for each integer passed.
A – 65 a - 97
Z – 90 z - 122
Example:
SELECT CHAR(65) ; Result : A
Date/Time Functions
Function Purpose Example Result
CURDATE( ) / Returns the current date Select CURDATE(); 2022-01-3
CURRENT_DATE( ) in YYYY- MM-DD format.
NOW( ) Returns the current date Select NOW(); 2022-01-3
& Time as YYYY-MM-DD 11:23:36
HH:MM:SS
SYSDATE( ) Returns the current date Select SYSDATE(); 2022-01-3
& Time as YYYY-MM-DD 11:23:36
HH:MM:SS
DATE( ) Returns the date part of a Select DATE(SYSDATE()); 2022-01-3
date- time expression.
MONTH( ) / Returns the Month/Year Select MONTH(‘2012-10-02’); 10
YEAR( ) from given date Select MONTH(SYSDATE()); 1
argument. Select YEAR(“2012-10-02”) 2012
DAYNAME( ) Returns the name of the Select DAYNAME(CURDATE()); Monday
weekday
DAYOFMONTH( ) Returns the day of month Select 20
(1-31). DAYOFMONTH(CURDATE());
DAYOFWEEK( ) Returns the day of week Select 6
(1-Sunday, 2-Monday,….., DAYOFWEEK(CURDATE());
7-Saturday)
DAYOFYEAR( ) Returns the day of Select 325
year(1-366). DAYOFYEAR(CURDATE());
AGGREGATE FUNCTIONS /GROUP FUNCTIONS
These functions work on particular column and applied together on all values under
that column.
min() – Find
minimum among all
values in a column
max()– Find
maximum among all
values in a column
sum ( ) – Find sum of
all values in a column
avg( ) – Find average of all values in a column
count()–
KVS REGIONAL OFFICE,Find count all values in a12
JAIPUR column
IP TERM-2 (2021-22) 5
1. How many employees are there in the company
SELECT COUNT(*) FROM emp;
MySQ
L
Functi
ons
Loveje
et
Arora
12th IP
2. Display maximum and minimum salary paid
SELECT MAX(Sal), MIN(Sal) FROM emp;
3. Display average and minimum
salary for the female employees
SELECT AVG(Sal), MIN(Sal)
FROM emp WHERE Sex = ‘F’;
4. What is the total salary
paid to sales
department. SELECT
SUM(Sal) FROM emp
WHERE Dname =
“Sales”;
5. Display total number of
male employees in the
company.SELECT
COUNT(*) FROM emp
WHERE Sex= ‘M’;
FROM STUDENT TABLE
1. Count total number of students, who have assigned marks
SELECT COUNT(Marks) FROM student; <= 4 (Doesn’t count NULL values, if
available in Marks column)
SELECT COUNT(*) FROM student; <= 5 (Count all rows
despite of any NULL value present inany column or not)