-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_3.sql
More file actions
63 lines (55 loc) · 1.84 KB
/
assignment_3.sql
File metadata and controls
63 lines (55 loc) · 1.84 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. Show employees along with their managers
ALTER TABLE employees
ADD COLUMN manager_id INT;
ALTER TABLE employees
ADD CONSTRAINT fk_manager
FOREIGN KEY (manager_id) REFERENCES employees(employee_id);
-- employee → manager (both inside employees)
UPDATE employees SET manager_id = 2 WHERE employee_id = 1; -- Aarav → Neha
UPDATE employees SET manager_id = NULL WHERE employee_id = 2;
UPDATE employees SET manager_id = NULL WHERE employee_id = 3;
-- To verify the result
SELECT
e.employee_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.employee_id;
-- Query 2. Find employees earning more than average salary
SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
-- Query 3. Display employee name, department, and company
SELECT
e.first_name || ' ' || e.last_name AS employee,
d.name AS department,
c.name AS company
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN companies c ON d.company_id = c.company_id;
-- Query 4. Identify the second-highest salary
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
-- Query 5. Rank employees by salary within departments
SELECT
e.first_name || ' ' || e.last_name AS employee,
d.name AS department,
e.salary,
RANK() OVER (
PARTITION BY e.department_id
ORDER BY e.salary DESC
) AS salary_rank
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
-- Query 6. Categorize employees as Low / Medium / High earners
SELECT
e.first_name || ' ' || e.last_name AS employee,
e.salary,
CASE
WHEN e.salary < 60000 THEN 'Low'
WHEN e.salary BETWEEN 60000 AND 100000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees e;