@@ -86,3 +86,141 @@ FROM orders o
86
86
JOIN employees e
87
87
ON o .EmployeeID = e .EmployeeID
88
88
WHERE o .OrderID > 10400 ;
89
+
90
+ /* *********** Expert Level ************/
91
+
92
+ /* 1. Select the most expensive product*/
93
+ SELECT ProductID,ProductName,Price
94
+ FROM products
95
+ ORDER BY Price DESC
96
+ LIMIT 1 ;
97
+
98
+ /* 2. Select the second most expensive product*/
99
+ /* version 1*/
100
+ SELECT ProductID,ProductName,Price
101
+ FROM products
102
+ ORDER BY Price DESC
103
+ LIMIT 1 OFFSET 1 ;
104
+
105
+ /* version 2 (complex)*/
106
+ WITH
107
+ tbl1 AS (SELECT ProductID,ProductName,Price
108
+ FROM products
109
+ ORDER BY Price DESC
110
+ LIMIT 2 ),
111
+ tbl2 AS (SELECT ProductID,ProductName,Price
112
+ FROM products
113
+ ORDER BY Price DESC
114
+ LIMIT 1 )
115
+
116
+ SELECT tbl1 .ProductID ,tbl1 .ProductName ,tbl1 .Price
117
+ FROM tbl1
118
+ LEFT JOIN tbl2 ON tbl1 .ProductID = tbl2 .ProductID
119
+ WHERE tbl2 .ProductID IS NULL ;
120
+
121
+
122
+ /* 3. Select name and price of each product, sort the result by price in decreasing order*/
123
+ SELECT ProductID,ProductName,Price
124
+ FROM products
125
+ ORDER BY Price DESC ;
126
+
127
+ /* 4. Select 5 most expensive products*/
128
+ SELECT ProductID,ProductName,Price
129
+ FROM products
130
+ ORDER BY Price DESC
131
+ LIMIT 5 ;
132
+
133
+ /* 5. Select 5 most expensive products without the most expensive (in final 4 products)*/
134
+ SELECT ProductID,ProductName,Price
135
+ FROM products
136
+ ORDER BY Price DESC
137
+ LIMIT 4 OFFSET 1 ;
138
+
139
+
140
+ /* 6. Select name of the cheapest product (only name) without using LIMIT and OFFSET*/
141
+ WITH temp
142
+ AS (SELECT ProductID,ProductName,MIN (Price)
143
+ FROM products)
144
+ SELECT ProductName
145
+ FROM temp;
146
+
147
+
148
+ /* 7. Select name of the cheapest product (only name) using subquery*/
149
+ SELECT ProductName
150
+ FROM products
151
+ WHERE Price IN (
152
+ SELECT MIN (Price) FROM products
153
+ );
154
+
155
+ /* 8. Select number of employees with LastName that starts with 'D'*/
156
+ SELECT EmployeeID, LastName, FirstName
157
+ FROM employees
158
+ WHERE LastName LIKE ' D%' ;
159
+
160
+ /* BONUS : same question for Customer this time */
161
+ SELECT CustomerName, SUBSTRING_INDEX(CustomerName," " ,1 ) AS firstName, SUBSTRING_INDEX(CustomerName," " ,- 1 ) AS lastName
162
+ FROM customers
163
+ WHERE SUBSTRING_INDEX(CustomerName," " ,- 1 ) LIKE ' D%' ;
164
+
165
+ /* 9. Select customer name together with the number of orders made by the corresponding customer
166
+ sort the result by number of orders in decreasing order*/
167
+ SELECT c .CustomerID , c .CustomerName , COUNT (* ) AS ' TotalOder'
168
+ FROM customers c
169
+ JOIN Orders o
170
+ ON c .CustomerID = o .CustomerID
171
+ GROUP BY c .CustomerID
172
+ ORDER BY 3 DESC , 1 ASC ;
173
+
174
+ /* 10. Add up the price of all products*/
175
+ SELECT SUM (Price)
176
+ FROM products;
177
+
178
+ /* 11. Select orderID together with the total price of that Order, order the result by total price of order in increasing order*/
179
+ SELECT od .OrderID , SUM ((od .Quantity * p .Price )) AS TotalValueOfOrder
180
+ FROM order_details od
181
+ JOIN products p ON p .ProductID = od .ProductID
182
+ GROUP BY 1
183
+ ORDER BY 2 ASC ;
184
+
185
+ /* 12. Select customer who spend the most money*/
186
+ SELECT c .CustomerID , c .CustomerName , SUM (od .Quantity * p .Price ) AS TotalSpending
187
+ FROM orders o
188
+ JOIN customers c ON o .CustomerID = c .CustomerID
189
+ JOIN order_details od ON o .OrderID = od .OrderID
190
+ JOIN products p ON p .ProductID = od .ProductID
191
+ GROUP BY c .CustomerID
192
+ ORDER BY 3 DESC
193
+ LIMIT 1 ;
194
+
195
+ /* 13. Select customer who spend the most money and lives in Canada*/
196
+ SELECT c .CustomerID , c .CustomerName , SUM (od .Quantity * p .Price ) AS TotalSpending, c .Country
197
+ FROM orders o
198
+ JOIN customers c ON o .CustomerID = c .CustomerID
199
+ JOIN order_details od ON o .OrderID = od .OrderID
200
+ JOIN products p ON p .ProductID = od .ProductID
201
+ WHERE c .Country LIKE ' Canada'
202
+ GROUP BY c .CustomerID
203
+ ORDER BY 3 DESC
204
+ LIMIT 1 ;
205
+
206
+ /* 14. Select customer who spend the second most money*/
207
+ SELECT c .CustomerID , c .CustomerName , SUM (od .Quantity * p .Price ) AS TotalSpending
208
+ FROM orders o
209
+ JOIN customers c ON o .CustomerID = c .CustomerID
210
+ JOIN order_details od ON o .OrderID = od .OrderID
211
+ JOIN products p ON p .ProductID = od .ProductID
212
+ GROUP BY c .CustomerID
213
+ ORDER BY 3 DESC
214
+ LIMIT 1 OFFSET 1 ;
215
+
216
+ /* 15. Select shipper together with the total price of proceed orders*/
217
+ SELECT o .ShipperID , shp .ShipperName , SUM (od .Quantity * p .Price ) AS TotalValueOfOrder
218
+ FROM orders o
219
+ JOIN order_details od ON o .OrderID = od .OrderID
220
+ JOIN products p ON p .ProductID = od .ProductID
221
+ JOIN shippers shp ON shp .ShipperID = o .ShipperID
222
+ GROUP BY 1
223
+ ORDER BY 2 ;
224
+
225
+
226
+
0 commit comments