-
Notifications
You must be signed in to change notification settings - Fork 1
/
searches_example.sql
122 lines (109 loc) · 2.23 KB
/
searches_example.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
-- Search the stock of each product in the Product table
SELECT
ProductName,
SUM(Stock) AS TotalStock
FROM
Product
GROUP BY
ProductName;
-- Search the products with a price greater than 100.00
SELECT
ProductName,
Price
FROM
Product
WHERE
Price > 100.00;
-- Search the customers with the first name John or the last name Doe
SELECT
CustomerID,
FirstName,
LastName,
Email
FROM
Customer
WHERE
FirstName = 'John' OR LastName = 'Doe';
-- Search the orders made on 2023-08-01
SELECT
OrderID,
CustomerID,
OrderDate,
Total
FROM
"Order"
WHERE
OrderDate = '2023-08-01';
-- Search the order items with a quantity equal or greater than 1
SELECT
OrderID,
ProductID,
Quantity,
UnitPrice
FROM
OrderItem
WHERE
Quantity >= 1;
-- Search the total sold of each product using JOIN
SELECT
Product.ProductName,
SUM(OrderItem.Quantity) AS TotalSold
FROM
OrderItem
JOIN
Product ON OrderItem.ProductID = Product.ProductID
GROUP BY
Product.ProductName;
-- Search the total spent by each customer using JOIN
SELECT
Customer.FirstName,
Customer.LastName,
SUM("Order".Total) AS TotalSpent
FROM
"Order"
JOIN
Customer ON "Order".CustomerID = Customer.CustomerID
GROUP BY
Customer.FirstName, Customer.LastName;
-- Search the products with the word 'keyboard' in the description
SELECT
ProductName,
Description
FROM
Product
WHERE
Description LIKE '%keyboard%';
--
SELECT
"Order".OrderID,
COUNT(OrderItem.OrderItemID) AS NumberOfItems
FROM
"Order"
JOIN
OrderItem ON "Order".OrderID = OrderItem.OrderID
GROUP BY
"Order".OrderID;
-- Search the customers who have not placed any orders using LEFT JOIN
SELECT
Customer.CustomerID,
Customer.FirstName,
Customer.LastName
FROM
Customer
LEFT JOIN
"Order" ON Customer.CustomerID = "Order".CustomerID
WHERE
"Order".OrderID IS NULL;
-- Search the top 5 products that have been sold the most
SELECT
Product.ProductName,
SUM(OrderItem.Quantity) AS TotalSold
FROM
OrderItem
JOIN
Product ON OrderItem.ProductID = Product.ProductID
GROUP BY
Product.ProductName
ORDER BY
TotalSold DESC
LIMIT 5;