-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise-employee.sql
80 lines (69 loc) · 1.98 KB
/
Exercise-employee.sql
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
75
76
77
78
79
80
CREATE TABLE Departments
(
ID INTEGER PRIMARY KEY,
NAME VARCHAR(20)
);
CREATE TABLE Employees
(
ID INTEGER PRIMARY KEY,
Name VARCHAR(20),
Salary INTEGER,
DEPARTMENT_ID INTEGER,
FOREIGN KEY (DEPARTMENT_ID) REFERENCES Departments(ID)
);
CREATE TABLE Commissions
(
ID INTEGER PRIMARY KEY,
EMPLOYEE_ID INTEGER,
COMMISSION_AMOUNT INTEGER,
FOREIGN KEY (EMPLOYEE_ID) REFERENCES Employees(ID)
);
CREATE UNIQUE INDEX index_id_dept ON Departments (ID);
CREATE UNIQUE INDEX index_id_emp ON Employees (ID);
CREATE UNIQUE INDEX index_id_comm ON Commissions (ID);
INSERT INTO Departments
VALUES (1,'Banking'),
(2,'Insurance'),
(3,'Services');
INSERT INTO Employees
VALUES (1,'Chris Gayle',1000000,1),
(2,'Michael Clarke',800000,2),
(3,'Rahul Dravid',700000,1),
(4,'Ricky Pointing',600000,2),
(5,'Albie Morkel',650000,2),
(6,'Wasim Akram',750000,3);
INSERT INTO Commissions
VALUES (1,1,5000),
(2,2,3000),
(3,3,4000),
(4,1,4000),
(5,2,3000),
(6,4,2000),
(7,5,1000),
(8,6,9000);
SELECT emp.name, sum(comm.COMMISSION_AMOUNT)
FROM employees AS emp
JOIN Commissions AS comm ON emp.ID = comm.employee_ID
GROUP BY comm.employee_ID
HAVING sum(comm.COMMISSION_AMOUNT)>=(
SELECT sum(comm.COMMISSION_AMOUNT)
FROM Commissions AS comm
GROUP BY comm.employee_id ORDER BY comm.COMMISSION_AMOUNT DESC LIMIT 1);
SELECT name,salary
FROM employees
WHERE salary =(
SELECT DISTINCT salary FROM employees
ORDER BY salary DESC LIMIT 3,1);
SELECT dept.name,sum(comm.Commission_amount)
FROM Commissions AS comm
JOIN Employees AS emp ON comm.employee_id=emp.id
JOIN Departments AS dept ON emp.department_id=dept.id
GROUP BY comm.employee_id
HAVING sum(comm.commission_amount)>=(
SELECT sum(comm.Commission_amount) FROM Commissions AS comm
GROUP BY comm.employee_id ORDER BY COMM.Commission_amount DESC LIMIT 1 );
SELECT CONCAT(group_CONCAT(emp.name order by emp.name),' ',comm.Commission_amount) AS Employees
FROM Commissions AS comm
JOIN employees AS emp ON comm.employee_id=emp.id
WHERE comm.commission_amount>3000
GROUP BY comm.Commission_amount