File tree 1 file changed +38
-0
lines changed 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -102,3 +102,41 @@ SELECT
102
102
max_raise_2(employee_id)
103
103
FROM employees
104
104
WHERE employee_id = 206 ;
105
+
106
+ -- max_raise_3 function
107
+ CREATE OR REPLACE FUNCTION max_raise_3 (empl_id INT )
108
+ RETURNS NUMERIC (8 ,2 ) AS $$
109
+ DECLARE
110
+ selected_employee employees%ROWTYPE;
111
+ selected_job jobs%ROWTYPE;
112
+ possible_raise NUMERIC (8 ,2 );
113
+ BEGIN
114
+ -- Take the job position and the salary
115
+ SELECT *
116
+ FROM employees INTO selected_employee
117
+ WHERE employee_id = empl_id;
118
+
119
+ -- Take the max salary based on his job position
120
+ SELECT *
121
+ FROM jobs INTO selected_job
122
+ WHERE job_id = selected_employee .job_id ;
123
+
124
+ -- Calculations
125
+ possible_raise = selected_job .max_salary - selected_employee .salary ;
126
+
127
+ IF (possible_raise < 0 ) THEN
128
+ RAISE EXCEPTION ' Person with salary greater than max_salary: %' , selected_employee .first_name ;
129
+ -- possible_raise = 0;
130
+ END IF;
131
+
132
+ RETURN possible_raise;
133
+ END;
134
+ $$ LANGUAGE plpgsql;
135
+
136
+ SELECT
137
+ employee_id,
138
+ first_name,
139
+ salary,
140
+ max_raise_3(employee_id)
141
+ FROM employees
142
+ WHERE employee_id = 206 ;
You can’t perform that action at this time.
0 commit comments