How to create functions in SQL?SQL has many built-in functions for performing the calculation of data. SQL provides built-in functions to perform the operations. Some useful functions of SQL are performing the mathematical calculations, string concatenation and sub-string etc. SQL functions are divided into two parts:
SQL Aggregate FunctionsSQL Aggregate functions return a single value which is calculated from the values.
SQL Scalar functionsSQL Scalar functions returns the single value according to the input value. Scalar functions:
Aggregate FunctionsThe aggregate functions return a single value after performing calculations on the group of values. Some of Aggregate functions are explained below. AVG FunctionAVG () returns the average value of the database after calculating the values in numeric column. Syntax : Using AVG() function Consider the following Emp table: The following SQL calculates the average salary of the employees. Result: COUNT() FunctionCount returns the number of rows which are present in the database, and either it is based on the condition or without condition. Its basic syntax is, Using COUNT() function Consider the following Emp table: SQL query to count the number of rows that satisfies the condition. Output: Example of COUNT (distinct) Consider the following Emp table: The SQL query is: Output: FIRST() FunctionThe function returns the first value of the specified column. Syntax: Using FIRST() function Consider the following Emp table: The SQL query will be: Output: LAST() FunctionThe LAST function returns the return last value of the selected column. Syntax of the LAST function is: Using LAST() function Consider the following Emp table: SQL Query is: Output: MAX() FunctionMAX() function returns the maximum value from the selected column of the table. Syntax: Consider the following Emp table: The following SQL query fetch the maximum salary. Output: MIN() FunctionMIN function returns the minimum value of selected column. The syntax of MIN function: Using MIN () function Consider the below Emp table: SQL query to find the minimum salary: Output: SUM() FunctionSUM () function returns the total of the specified columns. The syntax for SUM: See the following Emp table Sum of salaries are: Output: Scalar FunctionsScalar functions return a single value from an input value. Some of the Scalar functions are given below: UCASE () FunctionUCASE () converts the value of the string column into the Uppercase (Capital) characters. Syntax Using UCASE() function Consider the below Emp table: SQL query of UCASE: Result: LCASE() FunctionLCASE() function is used to convert the value of string columns to Lowercase. The syntax for LCASE: Using LCASE() functionConsider the following Emp table SQL query for converting the string value to Lowercase: Output: MID() FunctionMID() function is used to extract substrings from column values in the table. The syntax for the MID function is: Using MID() function Consider the following Emp table: The following SQL query returns the substring start from the second character. Output: ROUND() FunctionThe ROUND() function is used to round a numeric field to a number of the nearest integer. It is used for decimal point. Syntax: Using ROUND() function Consider the following Emp table: The following SQL query rounds the amount of salary column. Output:
Next TopicHow to run SQL Script
|