0% found this document useful (0 votes)
26 views6 pages

SQL Functions ONE SHOT - Lovejeet Arora 12IP

The document provides an overview of various MySQL functions categorized into Numeric, String, Date & Time, and Aggregate Functions. Each category includes specific functions with explanations and examples demonstrating their usage. The document serves as a guide for understanding and utilizing these functions in MySQL queries.

Uploaded by

amd533313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

SQL Functions ONE SHOT - Lovejeet Arora 12IP

The document provides an overview of various MySQL functions categorized into Numeric, String, Date & Time, and Aggregate Functions. Each category includes specific functions with explanations and examples demonstrating their usage. The document serves as a guide for understanding and utilizing these functions in MySQL queries.

Uploaded by

amd533313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MySQL Functions

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

KVS REGIONAL OFFICE, JAIPUR 12 IP TERM-2 (2021-22) 2


MySQL Functions
Lovejeet Arora
12th IP
CHARACTER/STRING FUNCTIONS

LENGTH() : Returns the length of a string in bytes/number of characters in string.


Example:
SELECT LENGTH(‘INFORMATICS’); Result:11

CHAR() : Returns the corresponding ASCII character for each integer passed.
A – 65 a - 97
Z – 90 z - 122
Example:
SELECT CHAR(65) ; Result : A

CONCAT(): Returns concatenated string i.e. it adds two strings.


Example:
SELECT CONCAT(‘Informatics’,‘Practices’);
Result : ‘Informatics Practices’

INSTR(): Returns the index of the first occurrence of substring.


Example:
SELECT INSTR(‘Informatics’,’ mat’);
Result : 6 (since ‘m’ of ‘mat’ is at 6th place)

LOWER()/ LCASE(): Returns the argument after converting it in lowercase.


Example: SELECT LOWER(‘INFORMATICS’); Result : informatics
UPPER()/ UCASE(): Returns the argument after converting it in uppercase.
Example:
SELECT UCASE(‘informatics’); Result : INFORMATICS
LEFT() : Returns the given number of characters by extracting them from the left side of the
string
Example: SELECT LEFT(‘INFORMATICS PRACTICES’, 3); Result : INF
RIGHT(): Returns the given number of characters by extracting them from the right side of the
string.
Example:
SELECT RIGHT(‘INFORMATICS PRACTICES’,3); Result : CES
MID(): Returns a substring starting from the specified position in a given string.
Example:
SELECT MID(‘INFORMATICS PRACTICES’,3,4); Result : FORM
SUBSTR() : Returns a substring from a given string.
Example:
SELECT SUBSTR(‘INFORMATICS’ , 3 , 4 ) ; Result : FORM
LTRIM() : Removes leading spaces.
Example:
SELECT LTRIM(' INFORMATICS'); Result: INFORMATICS
RTRIM(): Removes trailing spaces.
Example :
SELECT RTRIM('INFORMATICS '); Result:INFORMATICS
KVS REGIONAL OFFICE, JAIPUR 12 IP TERM-2 (2021-22) 3
MySQL Functions
Lovejeet Arora
12th IP
TRIM() : Removes leading and trailing spaces.
Example:
SELECT TRIM(' INFORMATICS '); Result:'INFORMATICS’

KVS REGIONAL OFFICE, JAIPUR 12 IP TERM-2 (2021-22) 4


MySQ
L
Functi
ons
Loveje
et
Arora
12th IP

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)

KVS REGIONAL OFFICE, JAIPUR 12 IP TERM-2 (2021-22) 6

You might also like