-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComp_Data_1_Part7
64 lines (53 loc) · 1.82 KB
/
Comp_Data_1_Part7
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
Hi, now we will see Nested Queries, ON DELETE(deleting entries from database, when they have foreign key associated to them) etc.
----Find names of all employees who have sold over 30000 to a single client.
SELECT employee.first_name,employee.last_name
FROM employee
WHERE employee.emp_id IN(
SELECT works_with.emp_id
FROM works_with
WHERE works_with.total_sales>30000
);
--------Find all clients who are handled by the branch that Michael Scott manages. Very interesting!!!
SELECT client.client_name
FROM client
WHERE client.branch_id IN(
SELECT branch.branch_id
FROM branch
WHERE branch.mgr_id IN(
SELECT employee.emp_id
FROM employee
WHERE (employee.first_name,employee.last_name)=('Michael','Scott')
)
);
---Find the names of employees who work with clients handled by the scranton branch. You do not know the branch id.----
SELECT employee.first_name,employee.last_name
FROM employee
WHERE employee.emp_id IN(
SELECT works_with.emp_id
FROM works_with
WHERE works_with.client_id IN(
SELECT client.client_id
FROM client
WHERE client.branch_id IN(
SELECT branch.branch_id
FROM branch
WHERE branch.branch_name='Scranton'
)
)
)
ORDER BY employee.emp_id;
-----Find the names of all clients who have spent more than 100,000 dollars.
SELECT client.client_name
FROM client
WHERE client.client_id IN (
SELECT client_id
FROM (
SELECT SUM(works_with.total_sales) AS totals, client_id
FROM works_with
GROUP BY client_id) AS total_client_sales
WHERE totals > 100000
);
######ON DELETE
DELETE FROM branch
WHERE branch_id=2;
SELECT * FROM branch_supplier;