-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_5.sql
More file actions
75 lines (64 loc) · 2.09 KB
/
assignment_5.sql
File metadata and controls
75 lines (64 loc) · 2.09 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
64
65
66
67
68
69
70
71
72
73
74
-- Query 1. Perform a salary transfer using transactions
BEGIN;
-- debit
UPDATE employees
SET salary = salary - 5000
WHERE emp_id = 10;
-- credit
UPDATE employees
SET salary = salary + 5000
WHERE emp_id = 12;
COMMIT;
-- Rollback if transaction version fails during testing or doen't executs other subqueries
ROLLBACK;
-- Query 2. Identify departments that have employees
SELECT d.department_id, d.name
FROM departments d
WHERE EXISTS (
SELECT 1 FROM employees e
WHERE e.department_id = d.department_id
);
-- or alternative method to do that:
SELECT d.department_id, d.name, COUNT(e.emp_id) AS total_employees
FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id
GROUP BY d.department_id, d.name
HAVING COUNT(e.emp_id) > 0;
-- Query 3. Display employee-manager hierarchy
SELECT
e.emp_id,
e.first_name || ' ' || e.last_name AS employee,
m.first_name || ' ' || m.last_name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.emp_id;
-- Query 4. Compare previous and next salaries
SELECT
emp_id,
first_name || ' ' || last_name AS employee,
salary,
LAG(salary) OVER (ORDER BY salary) AS prev_salary,
LEAD(salary) OVER (ORDER BY salary) AS next_salary
FROM employees;
-- Query 5. Generate department-wise salary totals
SELECT
d.name AS department,
SUM(e.salary) AS total_salary,
AVG(e.salary) AS avg_salary,
COUNT(e.emp_id) AS total_employees
FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.name
ORDER BY total_salary DESC;
-- Query 6. Export database structure and data
-- If you wantt full copy of db, as tables, schemas, rows, constranits.
-- data + structure
pg_dump -U username -d dbname -F c > backup.dump
-- If just want to print the blueprint, like table, keys, but not rows
-- Structure Only
pg_dump -U username -d dbname -s > schema.sql
-- If you wanna data from db (inserts only)
-- Data Only
pg_dump -U username -d dbname -a > data.sql
-- If you want to do the reverse operation, or recreate db from dump than type this command
-- Restoration
pg_restore -U username -d newdb backup.dump