SQLFunctions
SQLFunctions
AVG()
AVG(): It returns the calculated average value from values of the selected numeric
column
Syntax of AVG() function:
Select AVG(column_name) From table_name;
Example of AVG() function:
Select AVG(Salary) AS AverageSalary From Employees;
COUNT(): This function is used to count the number of rows returned in a Select
Statement.
Syntax of COUNT() function:
Select COUNT(column_name) From table_name;
Example of COUNT() function:
Select COUNT(*) AS NumEmployees From Employees;
FIRST(): The function returns the first value of the selected column.
Syntax of FIRST() function:
Select FIRST(column_name) From table_name;
Example of FIRST() function:
Select FIRST(Employee_ID) AS FirstEmployee From Employees;
LAST(): The function returns the last value of the selected column.
Syntax of LAST() function:
Select LAST(column_name) From table_name;
Example of LAST() function:
Select LAST(Employee_ID) AS LastEmployee From Employees;
MAX(): The function returns the maximum value of the selected column
Syntax of MAX() function:
Select MAX(column_name) From table_name;
Example of MAX() function:
Select MAX(Salary) AS MaxSalary From Employees;
MIN(): The function returns the minimum value of the selected column.
Syntax of MIN() function:
Select MIN(column_name) From table_name;
Example of MIN() function:
Select MIN(Salary) AS MinSalary From Employees;
SUM(): The function returns the sum of the values of the selected column.
Syntax of SUM() function:
Select SUM(column_name) From table_name;
Example of SUM() function:
Select SUM(Salary) AS TotalSalary From Employees;
Scalar Functions
The scalar functions are based on user input and return a single value. Let’s understand
through scalar functions:
UCASE(): The function converts the value of a field to uppercase.
Syntax of UCASE() function:
Select UCASE(column_name) From table_name;
Example of UCASE() function:
Select UCASE(Ename) From Employees;
LCASE(): The function converts the value of a field to lowercase.
Syntax of LCASE() function:
Select LCASE(column_name) From table_name;
Example of LCASE() function:
Select LCASE(Ename) From Employees;
MID(): The function extracts texts from the text
Syntax of MID() function:
Select MID(column_name,start,length) FROM table_name;
Specifying the length is not compulsory here and the start represents the start position.
Example of MID() function:
Select MID(Ename, 1, 4) From Employees;
LEN(): The function returns the length of the specified value.
Syntax of LEN() function:
Select LENGTH(column_name) From table_name;
Example of LEN() function:
Select LENGTH(Ename) From Employees;
ROUND(): The function returns the round numeric value to the specified decimal places.
This arithmetic operation is performed considering IEEE 754 standard.
Syntax of ROUND() function:
Select ROUND(column_name, decimals) From table_name;
decimals in the syntax specify the number of decimals to be fetched.
Example of ROUND() function:
Select ROUND(Salary, 0) From Employees;
NOW(): The function returns the current date and time of the system.
Syntax of NOW() function:
Select NOW() From table_name;
Example of NOW() function:
Select Ename, NOW() From Employees;
FORMAT(): The function formats how a field is to be presented.
Syntax of FORMAT() function:
Select FORMAT(column_name, format) From table_name;
Example of FORMAT() function:
SelectEname, FORMAT(NOW(), 'YYYY-MM-DD') AS Date From
Employees;
CONCAT(): The function joins the values stored in different columns, or it can be used
to join two strings simply.
Syntax of CONCAT() function:
Select CONCAT(string_1, string_2,...., string_n) AS Alias_Name;
Select CONCAT(column_name1, column_name2,...., column_name_n)
From table_name;
Example of CONCAT() function:
Select CONCAT('Hello', ' Everyone') As Gesture;
SelectCONCAT(FirstName, LastName) AS EmployeeName From
Employee;
REPLACE(): The function replaces the occurrence of a specified value with the new
one.
Syntax of REPLACE() function:
Select REPLACE(Original_Value, Value_to_Replace, New_Value) AS
Alias_Name;
SelectREPLACE(Column_Name, Character/string_to_replace,
new_String/character ) AS Alias_Name FROM Table_Name;
Example of REPLACE() function:
Select REPLACE('APPSE', 'S', 'L');
SelectLastName, REPLACE(LastName, 'r', 'a') AS Replace_r_a From
Employees;
POSITION(): The function returns the position of the first occurrence of a specified
substring in a string.
Syntax of POSITION() function:
Select POSITION(substring IN string/column_name);
Example of POSITION() function:
Select POSITION("A" IN "APPLE") As Position;
Select POSITION("a" in FirstName) From employees;