forked from munkhbato/sql_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntermediateProblems.sql
103 lines (75 loc) · 2.24 KB
/
IntermediateProblems.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
USE Northwind;
/* Exercise 20 */
SELECT CategoryName, COUNT(*) AS TotalProducts
FROM Products
JOIN Categories
ON Products.CategoryID = Categories.CategoryID
GROUP BY CategoryName
ORDER BY TotalProducts DESC;
/* Exercise 21 */
SELECT Country, City, COUNT(*) AS TotalCustomers
FROM Customers
GROUP BY Country, City
ORDER BY TotalCustomers DESC;
/* Exercise 22 */
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
WHERE UnitsInStock < ReorderLevel
ORDER BY ProductID;
/* Exercise 23 */
SELECT
ProductID,
ProductName AS 'Product Name',
UnitsInStock AS 'Units In Stock',
UnitsOnOrder AS 'Units On Order',
ReorderLevel AS 'Reorder Level',
Discontinued
FROM Products
WHERE UnitsInStock + UnitsOnOrder <= ReorderLevel AND Discontinued = 'false';
/* Exercise 24 */
SELECT CustomerID, CompanyName, Region
FROM Customers
ORDER BY CASE WHEN Region IS NULL THEN 1 ELSE 0 END ASC, Region ASC, CustomerID;
/* Exercise 25 */
SELECT ShipCountry, AVG(Freight) AS AverageFreight
FROM Orders
GROUP BY ShipCountry
ORDER BY AverageFreight DESC
LIMIT 3;
/* Exercise 26 */
SELECT ShipCountry, AVG(Freight) AS AverageFreight
FROM Orders
WHERE OrderDate >= '20150101' AND OrderDate < '20160101'
GROUP BY ShipCountry
ORDER BY AverageFreight DESC
LIMIT 3;
/* Exercise 27 */
SELECT ShipCountry, AVG(Freight) AS AverageFreight
FROM Orders
WHERE OrderDate BETWEEN '20150101' AND '20160101'
GROUP BY ShipCountry
ORDER BY AverageFreight DESC;
/* Exercise 28 */
SELECT TOP 3 ShipCountry, AVG(Freight) AS AverageFreight
FROM Orders
WHERE OrderDate >= (DATEADD(year, -1, (SELECT MAX(OrderDate) FROM Orders)))
GROUP BY ShipCountry
ORDER BY AverageFreight DESC;
/* Exercise 29 */
SELECT e.EmployeeID, e.LastName, od.OrderID, p.ProductName, od.Quantity
FROM Orders AS o
JOIN OrderDetails AS od ON o.OrderID = od.OrderID
JOIN Products AS p ON od.ProductID = p.ProductID
JOIN Employees AS e ON o.EmployeeID = e.EmployeeID
ORDER BY o.OrderID, p.ProductID;
/* Exercise 30 */
SELECT c.CustomerID, o.CustomerID
FROM Customers AS c
LEFT JOIN Orders AS o
ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL;
/* Exercise 31 */
SELECT c.CustomerID, o.CustomerID
FROM Customers AS c
LEFT JOIN Orders AS o ON o.CustomerID = c.CustomerID AND o.EmployeeID = 4
WHERE o.CustomerID IS NULL;