-
Notifications
You must be signed in to change notification settings - Fork 0
/
AS3.sql
223 lines (187 loc) · 4.1 KB
/
AS3.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
--1.
SELECT DISTINCT City
FROM Customers
WHERE City IN (SELECT City FROM Employees)
--2.
--use sub-query
SELECT DISTINCT City
FROM Customers
WHERE City NOT IN (SELECT City FROM Employees)
--no sub-query
SELECT DISTINCT City
FROM Customers c
LEFT JOIN
Employees e
ON c.City != e.City
--3.
SELECT DISTINCT ProductID,
SUM(Quantity) OVER (PARTITION BY ProductID) AS "Total Quantity"
FROM [Order Details]
--4.
SELECT DISTINCT c.City,
SUM(od.Quantity) AS "Total Quantity"
FROM Customers c
LEFT JOIN
Orders o
ON c.CustomerID = o.CustomerID
LEFT JOIN
[Order Details] od
ON o.OrderID = od.OrderID
GROUP BY c.City
--5a
SELECT c.City
FROM Customers c
GROUP BY c.City
HAVING COUNT(c.City) >= 2
UNION
SELECT s.City
FROM Customers s
GROUP BY s.City
HAVING COUNT(s.City) > 2
--5b
SELECT c.City
FROM Customers c
WHERE c.City IN
(SELECT s.City, Count(s.City)
FROM Customers s
GROUP BY s.City
HAVING Count(s.City) >= 2)
--6.
SELECT DISTINCT c.City
FROM Customers c
INNER JOIN
Orders o
ON c.CustomerID = o.CustomerID
INNER JOIN
[Order Details] od
ON o.OrderID = od.OrderID
GROUP BY c.City, od.ProductID
HAVING COUNT(od.ProductID) >= 2
--7.
SELECT o.CustomerID
FROM Orders o
WHERE o.ShipCity NOT IN
(SELECT c.City FROM Customers c)
--8.
--9a
SELECT e.City
FROM Employees e
WHERE e.City
NOT IN
(SELECT o.ShipCity
FROM Orders o)
--9b
SELECT DISTINCT e.City
FROM Employees e
LEFT JOIN
Orders o
ON e.City = o.ShipCity
WHERE o.ShipCity IS NULL
--10.
SELECT *
FROM
(SELECT TOP 1 e.City, COUNT(o.OrderID) AS "Most_Order"
FROM Employees e
INNER JOIN Orders o
ON e.EmployeeID = o.EmployeeID
GROUP BY e.City) T1
LEFT JOIN
(SELECT TOP 1 c.City, COUNT(r.Quantity) AS "Most_Quantity"
FROM [Order Details] r
INNER JOIN
Orders d on r.OrderID = d.OrderID
INNER JOIN Customers c
ON c.CustomerID = d.CustomerID
GROUP BY c.City) T2
ON T1.City = T2.City
--12.
SELECT empid, deptid
FROM Employee
WHERE mgrid = 0
--13.
SELECT T1.deptid, T1.Count_of_Employees
FROM
(SELECT TOP 1
FROM
(SELECT deptid, COUNT(empid)
FROM Employee
GROUP BY deptid
ORDER BY COUNT(empid))) T1
INNER JOIN
(SELECT deptname, COUNT(*) AS "Employee_Count"
FROM dept
GROUP BY deptid
) T2
ON T1.deptid = T2.deptid
--14.
SELECT deptname, empid, salary
FROM
(SELECT d.deptname, e.empid, e.salary,
RANK() OVER
(PARTITION BY e.deptid
ORDER BY
e.salary DESC) AS rnk
FROM dept d, employee e
WHERE d.deptid = e.deptid)
WHERE rnk <= 3
ORDER BY deptname, rnk
--15. top 3 products from every city which were sold maxium
SELECT T2.ShipCity, T2.ProductID, T2.SumProductQuantity AS "Quantity"
FROM
(SELECT T1.ShipCity, T1.ProductID, T1.SumProductQuantity,
RANK() OVER (PARTITION BY T1.ShipCity ORDER BY T1.SumProductQuantity DESC) AS rnk
FROM
(SELECT od.ProductID, o.ShipCity, SUM(od.Quantity) AS "SumProductQuantity"
FROM Orders o
INNER JOIN [Order Details] od
ON o.OrderID = od.OrderID
GROUP BY od.ProductID, o.ShipCity) T1) T2
WHERE rnk <= 3
--16. Create the table above (include the creation and insert script as part of the answer) As the result set, show every combination without duplicates.
CREATE TABLE City_Distance (
City varchar(50),
Distance int
);
INSERT INTO City_Distance (City, Distance)
VALUES ('A', 80)
INSERT INTO City_Distance (City, Distance)
VALUES ('B', 150)
INSERT INTO City_Distance (City, Distance)
VALUES ('C', 180)
INSERT INTO City_Distance (City, Distance)
VALUES ('D', 220)
SELECT 'B-A' AS "City", T3.d2-T3.d1 AS "Distance"
FROM
(SELECT T1.Distance AS "d1", T2.Distance AS "d2" FROM (
(SELECT Distance
FROM
City_Distance
WHERE City = 'A') T1 CROSS JOIN
(SELECT Distance
FROM
City_Distance
WHERE City = 'B') T2) ) T3
UNION
SELECT 'C-B' AS "City", T3.d2-T3.d1 AS "Distance"
FROM
(SELECT T1.Distance AS "d1", T2.Distance AS "d2" FROM (
(SELECT Distance
FROM
City_Distance
WHERE City = 'B') T1 CROSS JOIN
(SELECT Distance
FROM
City_Distance
WHERE City = 'C') T2) ) T3
UNION
SELECT 'D-C' AS "City", T3.d2-T3.d1 AS "Distance"
FROM
(SELECT T1.Distance AS "d1", T2.Distance AS "d2" FROM (
(SELECT Distance
FROM
City_Distance
WHERE City = 'C') T1 CROSS JOIN
(SELECT Distance
FROM
City_Distance
WHERE City = 'D') T2) ) T3