-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL PROJECT.sql
More file actions
361 lines (269 loc) · 7.91 KB
/
SQL PROJECT.sql
File metadata and controls
361 lines (269 loc) · 7.91 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
CREATE TABLE Books (
Book_ID SERIAL PRIMARY KEY,
Title VARCHAR(100),
Author VARCHAR(100),
Genre VARCHAR(50),
Published_Year INT,
Price NUMERIC(10, 2),
Stock INT );
CREATE TABLE Customers (
Customer_ID SERIAL PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100),
Phone VARCHAR(15),
City VARCHAR(50),
Country VARCHAR(150) );
CREATE TABLE Orders (
Order_ID SERIAL PRIMARY KEY,
Customer_ID INT REFERENCES Customers(Customer_ID),
Book_ID INT REFERENCES Books(Book_ID),
Order_Date DATE,
Quantity INT,
Total_Amount NUMERIC(10, 2) );
SELECT * FROM Books;
SELECT * FROM Customers;
SELECT * FROM Orders;
-- 1) Retrieve all books in the "Fiction" genre:
SELECT * FROM BOOKS
WHERE genre='Fiction';
-- 2) Find books published after the year 1950:
SELECT * FROM BOOKS
WHERE published_year>'1950';
-- 3) List all customers from the Canada:
SELECT * FROM customers
WHERE country='Canada';
-- 4) Show orders placed in November 2023:
SELECT * FROM orders
WHERE order_date between '01-11-2023' and '30-11-2023';
-- 5) Retrieve the total stock of books available:
SELECT SUM(STOCK) AS total_stock_of_books
FROM BOOKS;
-- 6) Find the details of the most expensive book:
SELECT * FROM books
order by price DESC
LIMIT 1;
--OR--
SELECT * FROM Books
WHERE Price = (SELECT MAX(Price)
FROM Books);
-- 7) Show all customers who ordered more than 1 quantity of a book:
SELECT * FROM orders
WHERE quantity>1;
--OR--
SELECT c.Customer_ID,
c.Name,
o.Order_ID,
o.Quantity
FROM Customers c
JOIN Orders o
ON c.Customer_ID = o.Customer_ID
WHERE o.Quantity > 1;
-- 8) Retrieve all orders where the total amount exceeds $20:
SELECT * FROM orders
WHERE total_amount>20;
-- 9) List all genres available in the Books table:
SELECT DISTINCT genre from books;
-- 10) Find the book with the lowest stock:
SELECT * FROM books
order by stock ASC
LIMIT 1;
--OR--
SELECT * FROM Books
WHERE Stock = (SELECT MIN(Stock)
FROM Books);
-- 11) Calculate the total revenue generated from all orders:
SELECT sum(total_amount) AS tptal_revenue FROM orders;
SELECT * FROM Books;
SELECT * FROM Customers;
SELECT * FROM Orders;
-- Advance Questions :
-- 1) Retrieve the total number of books sold for each genre:
SELECT b.genre,sum(o.quantity) as total_book_sold
from orders o
join books b
on o.book_id=b.book_id
group by genre;
-- 2) Find the average price of books in the "Fantasy" genre:
select avg(price) as avg_fantasy_price
from books
where genre='Fantasy';
-- 3) List customers who have placed at least 2 orders:
select customer_id,count(order_id) as order_count
from orders
GROUP BY customer_id
HAVING count(order_id)>=2;
--OR--
SELECT c.Customer_ID,
c.Name,
COUNT(o.Order_ID) AS Total_Orders
FROM Customers c
JOIN Orders o
ON c.Customer_ID=o.Customer_ID
GROUP BY c.Customer_ID,c.Name
HAVING COUNT(o.Order_ID)>=2;
-- 4) Find the most frequently ordered book:
select book_id,count(order_id) as order_count
from orders
GROUP BY book_id
order by order_count desc
limit 1;
--OR--
SELECT b.Book_ID,
b.Title,
COUNT(o.Order_ID) AS Order_Count
FROM Books b
JOIN Orders o
ON b.Book_ID=o.Book_ID
GROUP BY b.Book_ID,b.Title
ORDER BY Order_Count DESC
LIMIT 1;
-- 5) Show the top 3 most expensive books of 'Fantasy' Genre :
select * from books
where genre='Fantasy'
order by price desc
limit 3;
-- 6) Retrieve the total quantity of books sold by each author:
select b.author,sum(o.quantity) as toatl_quantity_sold
from orders o
join books b
on o.book_id=b.book_id
GROUP BY b.author;
-- 7) List the cities where customers who spent over $30 are located:
select DISTINCT c.city,total_amount
from orders o
join customers c
on c.customer_id=o.customer_id
where total_Amount>30;
-- 8) Find the customer who spent the most on orders:
select c.customer_id,c.name,sum(total_amount) as total_sum_amount
from orders o
join customers c
on c.customer_id=o.customer_id
group by c.customer_id,c.name
order by total_sum_amount desc
limit 1;
-- 9) Calculate the stock remaining after fulfilling all orders
SELECT b.Book_ID,
b.Title,
b.Stock AS Original_Stock,
COALESCE(SUM(o.Quantity),0) AS Ordered_Quantity,
(b.Stock-COALESCE(SUM(o.Quantity),0)) AS Remaining_Stock
FROM Books b
LEFT JOIN Orders o
ON b.Book_ID=o.Book_ID
GROUP BY b.Book_ID,b.Title,b.Stock;
SELECT * FROM Books;
SELECT * FROM Customers;
SELECT * FROM Orders;
/* SOME OTHER TYPE OF QUESTIONS*/
-- 1. Display all books that cost more than $30 and have stock greater than 5
select * from books
where price>30
and stock>5;
-- 2. Find the total number of orders placed by each customer
SELECT Customer_ID,
COUNT(Order_ID) AS Total_Orders
FROM Orders
GROUP BY Customer_ID;
-- 3. Show customer names and the titles of books they ordered
select c.name,b.title
from customers c
join orders o
ON c.Customer_ID=o.Customer_ID
JOIN Books b
on o.book_id=b.book_id;
-- 4. Find the book with the second highest price
select * from books
order by price DESC
LIMIT 1 OFFSET 1;
-- 5. Retrieve all books that belong to either 'Fantasy' or 'Fiction'
select * from books
where genre='Fantasy' or genre='Fiction';
SELECT * FROM Books
WHERE Genre IN ('Fantasy','Fiction');
-- 6. Find customers who placed more than 2 orders
select c.customer_id,c.name,count(o.order_id) as total_orders
from customers c
join orders o
on c.customer_id=o.customer_id
group by c.customer_id,c.name
having count(o.order_id)>2;
-- 7. Calculate total revenue generated by each genre
select b.genre,sum(o.total_amount) as revenue
from books b
join orders o
on b.book_id=o.book_id
group by b.genre;
-- 8. Show books that have never been ordered
SELECT b.*
FROM Books b
LEFT JOIN Orders o
ON b.Book_ID=o.Book_ID
WHERE o.Book_ID IS NULL;
-- 9. Find the customer who spent the highest total amount
select c.customer_id,c.name,
sum(o.total_amount) as total_spent
from customers c
join orders o
on c.customer_id=o.customer_id
group by c.customer_id,c.name
order by total_spent desc
limit 1;
-- 10. Display the top 5 books with the highest stock
SELECT *
FROM Books
ORDER BY Stock DESC
LIMIT 5;
-- 11. Find the average price of books for each genre
SELECT Genre,
AVG(Price) AS Avg_Price
FROM Books
GROUP BY Genre;
-- 12. Show all customers who purchased more than one quantity in a single order
SELECT c.Name,o.Quantity
FROM Customers c
JOIN Orders o
ON c.Customer_ID=o.Customer_ID
WHERE o.Quantity>1;
-- 13. Find the author whose books sold the highest quantity
SELECT b.author,sum(o.Quantity) as total_sold
FROM books b
JOIN Orders o
ON b.book_ID=o.book_ID
group by b.author
order by total_sold Desc
limit 1;
-- 14. List all customers from cities where total spending exceeded $50
SELECT c.city,c.name,sum(o.total_amount) as spending
FROM Customers c
JOIN Orders o
ON c.Customer_ID=o.Customer_ID
group by c.name,c.city
having sum(o.total_amount)>50;
SELECT * FROM Books;
SELECT * FROM Customers;
SELECT * FROM Orders;
-- 15. Show the month that generated the highest sales
select date_part('month',order_date) as month,
sum(total_amount) as sales
from orders
group by date_part('month',order_date)
order by sales DESC
limit 1;
-- 16. Find books whose price is greater than the average book price
select * from books
where price>(select avg(price) from books);
-- 17. Display customers who never placed an order
SELECT c.*
FROM Customers c
LEFT JOIN Orders o
ON c.Customer_ID=o.Customer_ID
WHERE o.Customer_ID IS NULL;
-- 18. Find the top 3 best-selling books based on quantity sold
select b.title,sum(o.quantity) as total_sold
from books b
join orders o
on b.book_id=o.book_id
group by b.title
order by total_sold DESc
limit 3;