0% found this document useful (0 votes)
3 views5 pages

SQL Function Notes

The document outlines built-in SQL functions categorized into single row (scalar) and multiple row (aggregate) functions, detailing their usage and syntax. It describes various aggregate functions such as MAX(), MIN(), AVG(), SUM(), and COUNT(), along with the GROUP BY and HAVING clauses. Additionally, it covers mathematical, string, and date functions, providing examples and syntax for each type.

Uploaded by

Harish
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)
3 views5 pages

SQL Function Notes

The document outlines built-in SQL functions categorized into single row (scalar) and multiple row (aggregate) functions, detailing their usage and syntax. It describes various aggregate functions such as MAX(), MIN(), AVG(), SUM(), and COUNT(), along with the GROUP BY and HAVING clauses. Additionally, it covers mathematical, string, and date functions, providing examples and syntax for each type.

Uploaded by

Harish
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/ 5

Built-In Functions

SQL provides many built-in Functions to perform operations on data. These functions are useful while
performing mathematical calculations, string concatenations, sub-strings, etc.
According to the processing on the value of column SQL functions can be classified as below.
Two types of SQL Functions
Depending on their application in one or multiple rows, SQL functions are categorised as:
(i) Single Row functions: These are also known as Scalar functions. Single row functions are the one the
work on single row and return one output per row. For example, length and case conversion functions
are single row functions.
(ii) Multiple Row functions: Multiple row functions are also called Aggregate functions. Multiple row
functions work upon group of rows and return one result for the complete set of rows. They are also
known as Group Functions.
Aggregate Functions
⚫ An aggregate function performs a calculation on one or more values and returns a single value. We often
use aggregate functions with the GROUP By and HAVING clauses of the SELECT statement.
⚫ Except for count (*), aggregate functions totally ignore NULL values and considers all values in the present
in a column.
Some aggregate functions are as follows:
(i) MAX(): This function returns the maximum value in selected columns. MAX() function ignores NULL
values and considers all values in the calculation.
Syntax:
SELECT MAX(Column_Name) FROM Table_Name ;
(ii) MIN(): This function returns the minimum value in selected columns. MIN() function ignores NULL
values.
Syntax:
SELECT MIN(Column_Name) FROM Table_Name;
(iii) AVG(): This function calculates the average of specified column(s). It ignores NULL values.
Syntax:
SELECT AVG(Column_Name) FROM Table_Name;
(iv) SUM(): This function calculates the sum of all values in the specified columns. It accepts only the
expression
that evaluates to numeric values.
Syntax:
SELECT SUM(Column_Name) FROM Table_Name;
(v) COUNT(): This function returns the number of rows found in a set.
COUNT(*) function returns a number of rows in a specified table or view that includes the number of
duplicates and NULL values.
Syntax:
SELECT COUNT(*) FROM Table_Name;
➢ GROUP BY Clause
GROUP BY clause is used to group rows returned by SELECT statement into a specified rows or groups.
Syntax:
SELECT column 1, column 2, ..., Aggregate_function
(exp) FROM Table_Name
WHERE condition
GROUP BY Column_Name;
➢ Having Clause
HAVING clause is oftenly used with the GROUP BY rows based on a specified condition.
Syntax:
SELECT column 1, column 2, ..., Aggregate_function
(Exp) FROM Table_Name
GROUP BY Column_Name
HAVING condition;
According to the SQL Data Type functions can be classified as below.
⚫ Numeric Functions:
◼ For processing Number Data type.

⚫ String Functions:
◼ For processing on String Data type.
⚫ Conversion Functions:
◼ For Convert Data type from one data type to another.
⚫ Date Functions:
◼ For processing on Date Data
Type. Mathematical Functions
There are various built-in functions available in MySQL for mathematical calculations. These mathematical
functions
accept numeric value, perform pre-defined specific operations on it and also return numeric value in result.
Some mathematical functions used in MySQL are as follows:
(i) POWER( ): This function is used to get the power of the given values.
Syntax:
POWER (m, n)
⚫ Parameter:
m: It is a base value in the calculation
n: It is exponent value in the calculation
This function returns m raised to the nth power.
e.g.,
SELECT POWER (4, 3);
Output
POWER(4, 3)
64
(ii) ROUND( ): This function is used to round up the number to the upwards or downwards whichever
the nearest whole number.
Syntax:
ROUND (number)
If you want to get number with certain number of decimal places, you can also pass that number, and
use following syntax.
ROUND (number, decimal
place);

e.g.,
mysql>SELECT ROUND (56.567);
Output
ROUND (56.567)
57.0
e.g.,
mysql>SELECT ROUND (56.567, 2);
Output
ROUND (56.567, 2)
56.57

(iii) MOD( ): This function is used to return the remainder of one number or expression by dividing it
to another number or expression.
Syntax:
MOD(n, m)
Parameter:
n: number to be divided by
m m: number that will
divide n e.g.,
mysql > SELECT MOD(17, 3);
MOD(17, 3)
2

Text/String Functions
MySQL text functions manipulate the character string data effectively. Some text functions used in MySQL are
as follows:
(i) UCASE/UPPER function: UCASE () or UPPER () function is used to convert the string argument into
upper case characters.
Syntax:
UCASE(
str) OR
UPPER(
str) e.g.,
mysql > SELECT UPPER (‘Hello‘);
Output
UPPER (‘Hello‘)
HELLO
mysql > SELECT UCASE (‘Program‘);
Output
UCASE(‘Program‘)
PROGRAM
(ii) LCASE( )/LOWER(): This function is used to convert the characters of an argument string to the
lowercase characters:
Syntax:
LCASE(s
tr) OR
LOWER (Str)
e.g.,
mysql>SELECT LOWER(‘HELLO‘);
Output
LOWER(‘HELLO‘)
hello

(iii) MID( ): This function extracts a substring from a string and returns a string with given length and
position.
Syntax:
MID (str, pos,
len) e.g.,
SELECT MID(‘Python program‘, 3, 5);
Output
MID(‘Python program‘,3,5)
thon
(iv) SUBSTRING( )/SUBSTR( ): These functions are same as MID( ) function.
(v) LENGTH( ): This function is used to return the length of the specified string. It returns the length in
bytes. This function also includes all the blank spaces which are include in string.
Syntax:
LENGTH(str)
e.g.,
mysql > SELECT LENGTH(‘program‘);
Output
LENGTH(‘program‘)
7
(vi) LEFT( ): This function is used to return a specified number of characters from the left of the string.
The number of characters returned is determined by the second argument.
Syntax:
LEFT(str, len)
e.g.,
mysql > SELECT LEFT (‘Program‘, 4);
Output
LEFT(‘Program‘, 4)
Prog
(vii) RIGHT( ): This function is just opposite of LEFT( ) function. It is used to return a specified number of
characters from the right of the string. The number of characters returned is determined by the second
argument.
Syntax:
RIGHT (str, len)
e.g.,
mysql > SELECT RIGHT (‘Program‘, 4);
Output
RIGHT (‘Program‘, 4)

gram
(viii) INSTR( ): This function takes two arguments as str (string) and sub_str (sub string) and returns the
position of the first occurrence of a specified sub_str from a given str.
Syntax:
INSTR( str, sub_str)
e.g., mysql>SELECT INSTR(‘Python Program‘, ‘thon’) as Result;
Output
Result

3
(ix) LTRIM( ): This function takes a string argument and returns a new string with all the leading
space characters removed. Spaces in the middle or trailing spaces are not removed.
Syntax:
LTRIM(str)
e.g., mysql > SELECT LTRIM (‘ Python Program’) as Result;
Output
Result

Python Program
(x) RTRIM( ): This function takes a string argument and returns a new string with all the trailing
space characters removed. Spaces in the middle or leading space are not removed.
Syntax:
RTRIM (str)
e.g., mysql > SELECT RTRIM (‘ Python Program ’) as Result;
Output
Result

Python Program
(xi) TRIM( ): This functions enables you to remove leading and trailing white space from string.
Syntax:
TRIM
(str);
e.g.,
mysql > SELECT TRIM (‘ Python Program ’) as Result;
Output
Result
Python Program
➢ Date Functions: The date functions are used to perform some operations on date that is stored in the
database. Some common date functions are as follows:
(i) NOW( ): This function returns the current date and time in the configured time zone as a string,
or a number in the ‘YYYY-MM-DD HH: MM: SS’ or ‘YYYYMMDDHHMMSS‘ format.
Syntax:
NOW( )
e.g.,
mysql > SELECT NOW( );
Output
NOW( )

2020-07-30 14:15:38
(ii) DATE( ): This function extracts the date value from a date.
Syntax:
DATE
(date)
e.g.,
mysql > SELECT DATE (‘2020-07-25 03:16:43’);
Output
DATE (‘2020-07-25 03:16:43’)

2020-07-25
(iii) MONTH( ): This function returns the month for date, in the range 1 to 12 for January to December. If
it returns 0 then month part of the given date contains NULL.
Syntax:
MONTH (date)
e.g.,
mysql >SELECT MONTH (‘2020-07-28‘);
Output
MONTH (‘2020-07-28‘)

7
(iv) MONTHNAME( ): This function returns the full name of the month for given date.
Syntax:
MONTHNAME (date)
e.g., mysql > SELECT MONTHNAME (‘2020-07-28‘);
Output
MONTHNAME (‘2020-07-28‘)
July
(v) YEAR( ): This function returns the year of the given date. It returns a year value in the range 1000 to
9999. If the date is zero, it returns 0.
Syntax:
YEAR(da
te) e.g.,
mysql > SELECT YEAR (‘2020-07-28‘);
Output
YEAR (‘2020-07-28‘)
2020
(vi) DAY( ): This function returns the day of the month of a given date. If the date argument is zero, it
returns
0. In case, the date in NULL, this function returns NULL.
Syntax:
DAY(date)
e.g., mysql> SELECT DAY(“2003-03-24”);
Output
DAY (‘2003-03-24’)
24
(vii) DAYNAME date: It returns the name of the day from the date.
Syntax:
DAYNAME(date)

mysql>SELECT DAYNAME (‘2020-07-28‘);


Output
DAYNAME (‘2020-07-28’)
Tuesday

You might also like