-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
151 lines (124 loc) · 3.46 KB
/
Copy pathqueries.sql
File metadata and controls
151 lines (124 loc) · 3.46 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
-- Create Tables
Drop Table if EXISTS Books;
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
);
Drop Table if exists Customers;
CREATE TABLE Customers(
Customer_Id Serial PRIMARY KEY,
Name Varchar(100),
Email Varchar(100),
Phone Varchar(15),
City Varchar(50),
Country Varchar(150)
);
Drop Table if exists Orders;
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;
-- Export CSV files directly from Import/Export data
--Queries
--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 Canada
Select * from Customers
where country='Canada';
--4)Show Orders placed in November 2023
Select * from Orders
where order_date BETWEEN '2023-11-01' AND '2023-11-30';
--5)Retrieve Total Stock of Book Available
Select sum(stock) AS Total_stock
from Books;
--6)Find details of the most expensive Books
Select * from Books
Order by price DESC
LIMIT 1;
--7)Show all customers who ordered more than 1 quantity of a book
Select * from Orders
where quantity>1;
--8)Retrieve all orders where the total amount exceeds 20
Select * from Orders
where total_amount>20;
--9)List all genre available in the Books table
Select DISTINCT(genre) from Books
--10)Find book with the lowest stock
Select * from Books
Order by stock
LIMIT 1;
--11)Calculate the total revenue generated from all orders
Select sum(Total_amount) AS Total_Revenue
from Orders;
--Advanced Queries
--1)Retrieve total number of books sold for each genre
Select sum(o.quantity) AS Total_Books_Sold,b.genre 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_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;
--4)Find the most frequently Ordered Book
Select o.book_id,b.title,Count(o.Order_id) AS order_count
from orders o
join books b
on o.book_id=b.book_id
group by o.book_id,b.title
Order by count(o.order_id) DESC
LIMIT 1;
--5)Show the top 3 most expensive books of 'Fantasy' genre
Select book_id,title,price
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)
from Orders o
join books b
on b.book_id=o.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 customers c
join orders o
on c.customer_id=o.order_id
where o.Total_amount>30
--8)Find the customer who spent the most on orders
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;
--9)Calculate the stock remaining after fulfilling all orders
Select b.book_id,b.title,b.stock,COALESCE(SUM(o.quantity),0) AS ordered_quantity,
b.stock-COALESCE(SUM(o.quantity),0) AS Remained_Qty
from books b
Left join Orders o
on o.book_id=b.book_id
Group by b.book_id