SQL Function Ip
SQL Function Ip
Functions in SQL:
A function is used to perform some particular task and it returns zero or more
values as a result.
Functions can be applied to work on single or multiple records (rows) of a
table.
Depending on their application in one or multiple rows, SQL functions are
categorised as Single row functions and Aggregate functions.
NOTE: Math functions accept numeric value as input, and return a numeric value
as a result. String functions accept character value as input, and return either
character or numeric values as output. Date and time functions accept date and time
values as input, and return numeric or string, or date and time values as output.
Numeric Functions
Three commonly used numeric functions are POWER( ), ROUND( ) and
MOD( ).
EX: Calculate HRA as 20% of salary and display the result after rounding it off to one
decimal place.
Solution:
mysql> SELECT ROUND(0.10*SALARY,1) "HRA" FROM EMPLOYEE;
+---------+
| HRA |
+---------+
| 25000.0 |
| 15000.0 |
| 20000.0 |
| 12000.0 |
| 10000.0 |
| 20000.0 |
| 10000.0 |
| 20000.0 |
| 20000.0 |
| 15000.0 |
+---------+
10 rows in set (0.00 sec)
String Functions
String functions can perform various operations on alphanumeric data which are
stored in a table. They can be used to change the case (uppercase to lowercase or
vice-versa), extract a substring, calculate the length of a string and so on.
Note: To demonstrate the above string function we use following employee
table.
Ex(2): Display the length of the email and part of the email from the email ID
before the character „@‟. (Note - Do not print „@‟)
SELECT LENGTH(EMAIL), LEFT( EMAIL, INSTR(EMAIL,‟@‟) – 1)
FROM EMPLOYEE;
Note: The function INSTR will return the position of “@” in the email address. So to print
email id without “@” we have to use position -1.
Ex(3): Find the year of joining of employee.
As the first four character from date is year when employee join the job.
SELECT MID(DOJ,1,4) FROM EMPLOYEE;
Ex(4): Display emails after removing the domain name extension “.com” from
emails of the employee.
SELECT TRIM( “.com” FROM EMAIL ) FROM EMPLOYEE;
Ex(5): Display details of all the customers having yahoo emails only.
SELECT * FROM EMPLOYEE WHERE EMAIL LIKE “%yahoo%”;
EX OF TRIM ( ) FUNCTION:
1. Write a query to remove leading and trailing spaces from string:
“ school “
Sol: SELECT TRIM( “ school “);
2. Write a query to remove leading spaces from string: “ school “
Sol: SELECT TRIM( leading ' ' FROM " SCHOOL ");
Ex:
1. Write a query to display current date and time on your system.
Sol: SELECT NOW();
2. Write a query to display only current date on your system.
Sol: SELECT CURDATE();
3. Write a query to extract date from a given datetime value „2022-11-30 12:59:25‟.
Sol: SELECT DATE('2022-11-30 12:59:25');
4. Write a query to display the name of month for given date ‟01-12-2022‟.
SELECT MONTHNAME('2022-12-01');
5. Write a query to display the name of day for date 12 Jul 1995.
SELECT DAYNAME('1995-07-12');
6. Select the day, month number and year of joining of all employees.
Sol: SELECT DAY(DOJ), MONTH(DOJ), YEAR(DOJ) FROM EMPLOYEE;
7. If the date of joining is not a Sunday, then display it in the following format
"Wednesday, 12, July, 1995."
SELECT DAYNAME(DOJ), DAY(DOJ), MONTHNAME(DOJ),
YEAR(DOJ) FROM EMPLOYEE WHERE DAYNAME(DOJ) <>
"SUNDAY";
Aggregate Functions
Aggregate functions are also called multiple row functions.
These functions work on a set of records as a whole, and return a single value for each column
of the records on which the function is applied.
The following table will shows the differences between single row functions and multiple row
functions. And another table describes some of the aggregate functions along with their usage.
Note that column must be of numeric type.
Ex(2): To display did and sum of salary of those department where number
of employee is more than two.
Sol: SELECT DID, SUM(SALARY) FROM EMPLOYEE
GROUP BY DID HAVING COUNT(*) > 2;
Ex(5): To display name of employee from each city with maximum salary.
Sol: SELECT ENAME, CITY, SALARY AS MAX_SAL
FROM EMPLOYEE
GROUP BY CITY
HAVING MAX( SALARY );