-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_4.sql
More file actions
63 lines (53 loc) · 1.44 KB
/
assignment_4.sql
File metadata and controls
63 lines (53 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- Query 1. Update salary for a specific department
UPDATE employees
SET salary = salary * 1.10
WHERE department_id = (
SELECT department_id
FROM departments
WHERE name = 'Engineering'
);
-- Query 2. Remove employees from an obsolete department
DELETE FROM employees
WHERE department_id = (
SELECT department_id
FROM departments
WHERE name = 'Marketing'
);
-- Query 3. Create a view for high-earning employees
CREATE VIEW high_earners AS
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE salary > 100000;
SELECT * FROM high_earners;
-- Query 4. Add validation to ensure salary is always positive
ALTER TABLE employees
ADD CONSTRAINT chk_positive_salary
CHECK (salary > 0);
-- Query 5. Improve search performance using indexing
CREATE INDEX idx_employees_department
ON employees(department_id);
CREATE INDEX idx_employees_manager
ON employees(manager_id);
CREATE INDEX idx_employees_salary
ON employees(salary);
CREATE INDEX idx_employees_last_name
ON employees(last_name);
-- Query 6. Create a stored procedure for department-based queries
CREATE OR REPLACE PROCEDURE get_employees_by_department(dep TEXT)
LANGUAGE plpgsql
AS $$
BEGIN
SELECT
e.first_name || ' ' || e.last_name AS employee,
e.role,
e.salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.name = dep;
END;
$$;
CALL get_employees_by_department('Engineering');