-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_queries.sql
More file actions
163 lines (105 loc) · 3.38 KB
/
complex_queries.sql
File metadata and controls
163 lines (105 loc) · 3.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
-- ======================= without views ======================
-- With union
(SELECT ProductName FROM product WHERE QuantityInStock > 0)
UNION
(SELECT p.ProductName FROM product AS P
INNER JOIN inventory AS I ON P.ProductID = I.ProductID);
-- With intersect
/*
(SELECT ProductName FROM product WHERE QuantityInStock > 0)
INTERSECT
(SELECT DISTINCT P.ProductID FROM product AS P
INNER JOIN p_order_item as O ON P.ProductID = O.ItemID);
*/
SELECT P.ProductName FROM product as P inner join p_order_item as O
on P.ProductID = O.ItemID
where P.QuantityInStock >0;
-- with set difference
/*
(SELECT ProductName FROM product WHERE QuantityInStock > 0)
EXCEPT
(SELECT DISTINCT P.ProductID FROM product AS P
INNER JOIN p_order_item as O ON P.ProductID = O.ItemID);
*/
Select distinct P.ProductName from product as P inner join p_order_item as O
on P.ProductID = O.ItemID WHERE QuantityInStock > 0 and O.ItemID is NULL;
-- with division
SELECT CustomerID
FROM p_order
GROUP BY CustomerID
HAVING COUNT(DISTINCT ProductID) = (SELECT COUNT(*) FROM product);
-- =================== Join with views =================
-- innner join
create view supplierpaymentterms AS
SELECT P.PaymentTerms, S.SupplierName
FROM supplier_paymentterms AS P
INNER JOIN supplier as S ON P.SupplierID = S.SupplierID;
SELECT * FROM supplierpaymentterms;
-- natural join
create view Stocks AS
select I.QuantityInStock , W.Capacity
FROM inventory as I
natural join warehouse as W;
SELECT * FROM Stocks;
-- left outer join
create view Ordertoship AS
SELECT P.OrderDate, S.ShippingDate
FROM p_order as P
LEFT OUTER JOIN shipment AS S ON P.OrderID = S.OrderID;
SELECT * FROM Ordertoship;
-- right outer join
create view Suppliernumber AS
SELECT S.supplierID, T.telephone
FROM supplier as S
RIGHT OUTER JOIN supplier_telephone AS T ON S.SupplierID = S.SupplierID;
SELECT * FROM Suppliernumber;
-- full outer join
create view citypaymentterm AS
(SELECT S.supplierCity, P.paymentterms
FROM supplier as S
LEFT OUTER JOIN supplier_paymentterms AS P ON S.SupplierID = S.SupplierID)
UNION
(SELECT S.supplierCity, P.paymentterms
FROM supplier as S
LEFT OUTER JOIN supplier_paymentterms AS P ON S.SupplierID = S.SupplierID);
SELECT * FROM citypaymentterm;
-- =================== Nested Queries with relaitonal algibric operations ==================
-- with union
SELECT CustomerID
FROM p_order
WHERE ProductID IN (
(SELECT ProductID FROM product WHERE QuantityInStock > 0)
UNION
(SELECT DISTINCT P.ProductID FROM product AS P
INNER JOIN product_supplier AS PS ON P.ProductID = PS.ProductID)
);
-- with intersection
/*
(SELECT ProductName
FROM product
WHERE QuantityInStock > 0)
INTERSECT
(SELECT DISTINCT P.ProductID FROM product AS P
INNER JOIN product_supplier AS PS ON P.ProductID = PS.ProductID);
*/
SELECT DISTINCT P.ProductName
FROM product AS P
INNER JOIN product_supplier AS PS ON P.ProductID = PS.ProductID
WHERE P.QuantityInStock > 0;
-- with set difference
/*
(SELECT ProductName
FROM product
WHERE QuantityInStock > 0)
EXCEPT
(SELECT DISTINCT P.ProductID FROM product AS P
INNER JOIN product_supplier AS PS ON P.ProductID = PS.ProductID);
*/
SELECT DISTINCT P1.ProductName
FROM product AS P1
LEFT JOIN (
SELECT DISTINCT P2.ProductID
FROM product AS P2
INNER JOIN product_supplier AS PS ON P2.ProductID = PS.ProductID
) AS T ON P1.ProductID = T.ProductID
WHERE P1.QuantityInStock > 0 AND T.ProductID IS NULL;