Topic 4: SQL Functions
Relation: EMPLOYEE
Employee_ Last_ First_ Middle Job_ID Manager Hire_Date Salary Comm Department
ID Name Name _ _ID _ID
Name
7369 Smith John Q Null Null 17-DEC- 800 Null 20
84
7499 Allen Kevin J 670 7698 20-FEB-85 1600 575.25 30
7505 Doyle Jean K 671 7839 04-APR-85 2850 Null 30
7506 dennis Lynn S 671 7839 15-MAR- Null Null 30
85
7507 baker Leslie D 671 7839 10-JUN-85 2200 Null 40
7521 Wark Cynthia D 670 7698 22-FEB-85 1250 248.75 30
DROP TABLE EMPLOYEE;
CREATE TABLE EMPLOYEE (EMPLOYEE_ID INT, LAST_NAME VARCHAR(10),
FIRST_NAME VARCHAR(10), MIDDLE_NAME VARCHAR(2), JOB_ID INT, MANAGER_ID
INT, HIRE_DATE DATE, SALARY INT, COMM FLOAT(5,2), DEPARTMENT_ID INT);
INSERT INTO EMPLOYEE VALUES (7369, 'Smith', 'John', 'Q', Null, Null, '1984-12-17', 800,
Null, 20);
INSERT INTO EMPLOYEE VALUES (7499, 'Allen', 'Kevin', 'J', 670, 7698, '1985-02-20', 1600,
575.25, 30);
INSERT INTO EMPLOYEE VALUES (7505, 'Doyle', 'Jean', 'K', 671, 7839, '1985-04-04', 2850,
Null, 30);
INSERT INTO EMPLOYEE VALUES(7506, 'dennis', 'Lynn', 'S', 671, 7839, '1985-03-15', Null,
Null, 30);
INSERT INTO EMPLOYEE VALUES(7507, 'baker', 'Leslie', 'D', 671, 7839, '1985-06-10', 2200,
Null, 40);
INSERT INTO EMPLOYEE VALUES(7521, 'Wark', 'Cynthia', 'D', 670, 7698, '1985-02-22', 1250,
248.75, 30);
COMMIT;
Aggregate Functions:
1. Display the maximum salary of the Employee whose manager ID IS 7698.
2. Display the number of employees in the relation Employee.
3. Display the maximum salary of the Employee whose Job_ID is 671.
4. Display the average salary of the employees whose middle name is D.
5. Display the sum of the Salary of all employees who belong to the department 30.
Numeric Functions:
1. Display the remainder of Job_ID divided by 10.
2. Display the absolute, ceil and floor values of Commision.
3. Display the power of Department _ID raised to 3.
4. Display the sign of values in Manager_ID.
5. Display the value of 456.3859 rounded off to 2 decimal places.
6. Display the value of 789.25489 truncated to 3 decimal places.
7. Display the value of 23598.1548 rounded off to -1 decimal places.
8. Display the value of 98756.2145 truncated to -3 decimal places.
9. Display the value e^12.
10. Find the greatest and least value from [‘d’, ‘r’, ‘w’, ‘Q’, ‘A’].
String Functions:
1. Display the First Name of all Employees in lower case.
2. Display the length of Last name of all Employees.
3. Display the Position of character 'a' in Last_Name.
4. Display the first name of all employees in reverse.
5. Display the Employee ID of all the employees padding them to the right up to 5 characters
with ‘*’.
6. Display the ASCII value of Middle Name of all Employees.
7. Display the first letter of Last Name of all Employees in capital letters.
8. Change the characters from 3rd to 5th position in First_Name as ‘DB’.
9. Replace the String “en” as “een” in Last_Name.
10. Merge and Display the first name and lastname of employees whose salary is above 2000.
11. Display the position of the character a in the first name of all employees.
12. Display the last two characters of Last name of the employees whose Job ID is 670.
Date Functions:
1. Display the current date and time.
2. Display the day name in which the employees are hired.
3. Display the months between 21-MAR-1990 and Hire Date.
4. Display the details of the Employees who joined in the month of February.
5. Display, in which day of the month the employees whose department ID is 30 are hired.
6. Display the experience(in years) of the employees whose salary is above 2000.
7. Display the month of the hire date of the employees who does not get any commision.
8. Display the Employees whose salary is between 800 to 1500 and joined in the month of
December.
Null Functions:
1. Display lastname and commission of the employee, If the comm value is null then it should
be displayed as ‘No Commision’.
2. Fetch the name of all employees whose salary is available in the table.
3. Find the sum of salary of all Employees, if Salary is not available, use salary as 10000.
4. Display the difference between Manager ID and Employee ID where the comm value is not
null.
5. Display the First name, Last name, Salary and a Salary Grade based on these conditions for
all the employees.
● if the salary is between 0 and 5000 – salary grade is A
● if the salary is between 5001 and 15000 – salary grade is B
● if the salary is between 15001 and 20000 – salary grade is C
● for others – salary grade is D
6. Display the salary incremented by 500 if Job_ID is 670 else 1000.
7. Compare Last Name and First Name of all Employees, if they are equal return null else Last
Name.