Repository navigation
Expand file tree
/
Copy pathdb_solution_0x02.sql
More file actions
82 lines (75 loc) · 2.52 KB
/
Copy pathdb_solution_0x02.sql
File metadata and controls
82 lines (75 loc) · 2.52 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
75
76
77
78
79
80
81
82
-- (C) A.Voß, a.voss@fh-aachen.de, info@codebasedlearning.dev
-- SQL-Solutions Unit 0x02
-- default schema, i.e. unqualified table names refer to ami_zone
SET SEARCH_PATH = ami_zone;
-- A2.1:
SELECT * FROM shop_order;
SELECT * FROM shop_customer;
SELECT * FROM hr_employee;
-- a)
SELECT O.delivery_time "Delivery", C.brand "Customer"
FROM shop_order O,shop_customer C
WHERE O.customer_id = C.id;
-- b)
SELECT O.delivery_time "Delivery", C.brand "Customer"
FROM shop_order O
INNER JOIN shop_customer C on O.customer_id = C.id;
-- c)
SELECT O.delivery_time "Delivery", C.brand "Customer", E.name "Agent"
FROM shop_order O
INNER JOIN shop_customer C on O.customer_id = C.id
INNER JOIN hr_employee E on O.employee_id = E.id;
-- A2.2:
SELECT * FROM div_department;
SELECT * FROM div_location;
SELECT * FROM div_located_in;
-- a)
SELECT D.name, S.place
FROM div_department D
JOIN div_located_in LI on D.id = LI.department_id
JOIN div_location S on LI.location_id = S.id;
-- b)
SELECT D.name, L.place
FROM div_department D
LEFT OUTER JOIN div_located_in LI on D.id = LI.department_id
LEFT OUTER JOIN div_location L on LI.location_id = L.id;
-- c)
SELECT D.name, L.place
FROM div_location L
RIGHT OUTER JOIN div_located_in LI on LI.location_id = L.id
RIGHT OUTER JOIN div_department D on D.id = LI.department_id;
-- d)
SELECT D.name, L.place
FROM div_department D
LEFT OUTER JOIN div_located_in LI on D.id = LI.department_id
LEFT OUTER JOIN div_location L on LI.location_id = L.id
WHERE L.place is null;
-- A2.3:
SELECT * FROM hr_employee;
SELECT * FROM hr_works_in_at;
-- a)
SELECT E1.name, E2.name "Manager"
FROM hr_employee E1
JOIN hr_employee E2 on E1.employee_id = E2.id
WHERE E2.name = 'Mia';
-- b)
SELECT E1.name, D.name, T.name, W.hours_per_week, E2.name "Manager"
FROM hr_employee E1
JOIN hr_works_in_at W on E1.id = W.employee_id
JOIN div_department D on D.id = W.department_id
JOIN hr_task T on W.task_id = T.id
JOIN hr_employee E2 on E1.employee_id = E2.id;
-- A2.4:
SELECT * FROM shop_customer;
SELECT * FROM shop_connected_to;
-- a)
SELECT C1.brand, C2.brand
FROM shop_customer C1
JOIN shop_connected_to CT on C1.id = CT.customer_a_id
JOIN shop_customer C2 on C2.id = CT.customer_b_id
WHERE C1.brand ilike '%bank%' or C1.brand ilike '%kasse%'; -- ilike: also 'Deutsche Bank'
-- b)
SELECT C.brand
FROM shop_customer C
LEFT OUTER JOIN shop_connected_to CT on C.id = CT.customer_a_id
WHERE CT.customer_b_id is null;