In PostgreSQL, create a PL/pgSQL block that defines a function named calculate_salary_bonus which takes two parameters:employee_id and bonus_percentage. This function should calculate the bonus amount for the employee with the givenemployee_id based on their current salary and the provided bonus percentage. Assume “employee” table with fields – eid, enameand salary
Question
In PostgreSQL, create a PL/pgSQL block that defines a function named calculate_salary_bonus which takes two parameters:employee_id and bonus_percentage. This function should calculate the bonus amount for the employee with the givenemployee_id based on their current salary and the provided bonus percentage. Assume “employee” table with fields – eid, enameand salary
Solution
Sure, here is a step-by-step guide on how to create the function in PostgreSQL:
-
First, you need to define the function using the
CREATE OR REPLACE FUNCTIONstatement. This statement allows you to modify the function if it already exists or create a new one if it doesn't. -
Then, you specify the name of the function, which is
calculate_salary_bonusin this case. -
After that, you define the parameters that the function will take. The function takes two parameters:
employee_idandbonus_percentage. -
Then, you specify the return type of the function. In this case, the function will return a numeric value which represents the bonus amount.
-
Inside the function, you use a
SELECTstatement to get the salary of the employee with the givenemployee_idfrom theemployeetable. -
You calculate the bonus amount by multiplying the salary by the
bonus_percentageand divide by 100. -
Finally, you return the bonus amount.
Here is the PL/pgSQL code that implements the above steps:
CREATE OR REPLACE FUNCTION calculate_salary_bonus(employee_id INT, bonus_percentage NUMERIC)
RETURNS NUMERIC AS $$
DECLARE
employee_salary NUMERIC;
bonus_amount NUMERIC;
BEGIN
SELECT salary INTO employee_salary FROM employee WHERE eid = employee_id;
bonus_amount := (employee_salary * bonus_percentage) / 100;
RETURN bonus_amount;
END; $$
LANGUAGE plpgsql;
You can call this function using the following SQL statement:
SELECT calculate_salary_bonus(1, 10);
This will calculate and return the bonus amount for the employee with eid 1 and a bonus percentage of 10%.
Similar Questions
In PostgreSQL, create a PL/pgSQL block that defines a procedure namedupdate_employee_salary which takes two parameters: employee_id and new_salary. This procedure should update the salary ofthe employee with the given employee_id to the new salary value. Assume “employee” table with fields – eid, ename and salary
Create PL/pgSQL procedure for the increment of employees where in salary less than 35000 will get hike of 15% in their previoussalary and other will get 10% hike in their previous salary. Using following schema, Employees (id, name, department, salary)call the procedure by id and print employee’s name with their updated salary
Consider following schema and write SQL for given statements.title (id,designation,DOJ), bonus(id,bonus_date,amount)Create tables with Primary Key, foreign key constraints in given schemas.1)Retrieve the employees who haven't received any bonuses.2)Retrieve the total bonus amount received by each employee.3)Retrieve the highest bonus amount received.4)List out id's whose bonus amount is at most 4000 and designation is admin
A) Write PL/pgSQL function to increment the employee’s salary by 10% if his/her department is ‘HR’ for given(inputted)employee_id.Schema: Employee(employee_id, first_name, last_name, department, salary)B) Write PL/pgSQL function to find the number of Sundays between given dates
You are tasked with creating a Python program to automate salary calculations based on an employee's years of service and job designation. The program should prompt users to input their years of service and job designation, consider predefined salary structures for each designation, and calculate the final salary. Additionally, implement a feature to display the salary details, including any bonuses or allowances based on the years of service. Design a program that efficiently manages salary computation and provides transparency to employees.Predefined salary structure and Bonus for each designation :'Manager'->base_salary=80000, bonus_rate=0.1'Engineer'->base_salary=60000, bonus_rate=0.08'Technician'->base_salary=45000, bonus_rate=0.05Input Format:In first line, get the name of the employee in string through keyboardIn the second line, get the designation (Manager/Engineer/Technician) in string through keyboardIn the third line, get the year of service in integer through keyboard.Output Format:Display the given details in seperate lines."Name:", name of the employee"Designation:", designation"Years of Service:", year of service"Base Salary:", base_salary"Bonus:", based on bonus rate*year of service*base salary"Total Salary:", calculated total salary
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.