fork download
  1. create schema if not exists Online_shopping_system;
  2.  
  3. set search_path to Online_shopping_system;
  4.  
  5. drop table if exists customers;
  6. drop table if exists addresses ;
  7. drop table if exists categories ;
  8. drop table if exists orderitems ;
  9. drop table if exists orders ;
  10. drop table if exists payments ;
  11. drop table if exists products ;
  12. drop table if exists reviews ;
  13.  
  14. create table if not exists customers(
  15. customerid int primary key,
  16. name varchar(30),
  17. email varchar(30),
  18. phone varchar(30),
  19. registrationdate date
  20. );
  21. create table if not exists addresses(
  22. addressid int primary key,
  23. customerid int ,
  24. city varchar(20),
  25. country varchar(20),
  26. postalcode varchar(20),
  27. foreign key (customerid) references customers(customerid)
  28. );
  29.  
  30.  
  31. create table if not exists payments (
  32. paymentid int primary key,
  33. orderid int ,
  34. paymentdate date,
  35. amount int,
  36. method varchar(20),
  37. foreign key (orderid) references orders(orderid)
  38. );
  39.  
  40. create table if not exists categories(
  41. categoryid int primary key,
  42. categoryname varchar(20)
  43. );
  44.  
  45. create table if not exists products(
  46. productid int primary key,
  47. categoryid int,
  48. productname varchar(30),
  49. price int,
  50. stock int,
  51. foreign key (categoryid) references categories(categoryid)
  52. );
  53.  
  54.  
  55. create table if not exists orders(
  56. orderid int primary key,
  57. customerid int,
  58. addressid int,
  59. orderdate date,
  60. status varchar(20),
  61. foreign key (customerid) references customers(customerid),
  62. foreign key (addressid) references addresses(addressid)
  63. );
  64.  
  65. create table if not exists orderitems
  66. (
  67. orderid int,
  68. productid int,
  69. quantity int,
  70. unitprice int,
  71. primary key(orderid,productid),
  72. foreign key (orderid) references orders(orderid),
  73. foreign key(productid) references products(productid)
  74. );
  75.  
  76. create table if not exists reviews(
  77. reviewid int primary key,
  78. customerid int,
  79. productid int,
  80. rating int,
  81. comment varchar(200),
  82. reviewdate date,
  83. foreign key (customerid) references customers(customerid),
  84. foreign key (productid) references products(productid)
  85. );
  86.  
  87.  
  88. insert into customers values
  89. (1,'Ahmed Ali','ahmed@gmail.com','01011111111','2025-01-10'),
  90. (2,'Sara Mohamed','sara@gmail.com','01022222222','2025-02-15'),
  91. (3,'Omar Hassan','omar@gmail.com','01033333333','2025-03-20'),
  92. (4,'Mona Adel','mona@gmail.com','01044444444','2025-04-05'),
  93. (5,'Youssef Sami','youssef@gmail.com','01055555555','2025-05-12');
  94.  
  95.  
  96. INSERT INTO addresses VALUES
  97. (1,1,'Cairo','Egypt','11511'),
  98. (2,1,'Giza','Egypt','12511'),
  99. (3,2,'Alexandria','Egypt','21511'),
  100. (4,3,'Mansoura','Egypt','35511'),
  101. (5,4,'Cairo','Egypt','11512'),
  102. (6,5,'Tanta','Egypt','31511');
  103.  
  104.  
  105.  
  106. INSERT INTO categories VALUES
  107. (1,'Laptops'),
  108. (2,'Phones'),
  109. (3,'Accessories');
  110.  
  111.  
  112. INSERT INTO products VALUES
  113. (1,1,'Dell XPS 13',35000,10),
  114. (2,1,'HP Pavilion',28000,8),
  115. (3,2,'iPhone 15',50000,12),
  116. (4,2,'Samsung S24',42000,15),
  117. (5,3,'Wireless Mouse',500,50),
  118. (6,3,'Mechanical Keyboard',1500,30);
  119.  
  120.  
  121.  
  122. INSERT INTO orders VALUES
  123. (1,1,1,'2025-06-01','Delivered'),
  124. (2,2,3,'2025-06-05','Pending'),
  125. (3,1,2,'2025-06-07','Delivered'),
  126. (4,3,4,'2025-06-10','Shipped'),
  127. (5,5,6,'2025-06-12','Pending');
  128.  
  129.  
  130. INSERT INTO orderitems VALUES
  131. (1,1,1,35000),
  132. (1,5,2,500),
  133.  
  134. (2,3,1,50000),
  135.  
  136. (3,2,1,28000),
  137. (3,6,1,1500),
  138.  
  139. (4,4,1,42000),
  140.  
  141. (5,5,3,500),
  142. (5,6,2,1500);
  143.  
  144.  
  145.  
  146.  
  147. INSERT INTO payments VALUES
  148. (1,1,'2025-06-01',36000,'Credit Card'),
  149. (2,2,'2025-06-05',50000,'Cash'),
  150. (3,3,'2025-06-07',29500,'Credit Card'),
  151. (4,4,'2025-06-10',42000,'PayPal');
  152.  
  153.  
  154. INSERT INTO reviews VALUES
  155. (1,1,1,5,'Excellent laptop','2025-06-15'),
  156. (2,2,3,4,'Very good phone','2025-06-16'),
  157. (3,3,4,5,'Amazing camera','2025-06-17'),
  158. (4,1,5,4,'Good mouse','2025-06-18'),
  159. (5,5,6,5,'Great keyboard','2025-06-19');
  160.  
  161.  
  162. select * from orders;
  163.  
  164. --List all products with price > 100 ordered by price in descending order
  165. select* from products
  166. where price > 100
  167. order by price desc;
  168.  
  169. --Customers names and ids from a New York city
  170. select distinct name, customers.customerid
  171. from customers join addresses
  172. on customers.customerid=addresses.customerid
  173. where city='Cairo';
  174.  
  175. -- another solution
  176. select name,customerid
  177. from customers
  178. where customerid in (
  179. select customerid
  180. from addresses
  181. where city='Cairo'
  182. )
  183.  
  184. --Orders in the last 20 days
  185. select* from orders
  186. where orderdate>= current_date - interval '20 days';
  187.  
  188. --Reviews with rating >= 4 sorted by newest
  189. select*from reviews
  190. where rating >=4
  191. order by reviewdate desc;
  192.  
  193. --Show products with their category names
  194. select products.*,categoryname
  195. from products join categories
  196. on products.categoryid=categories.categoryid;
  197.  
  198. --Show payments with order date
  199. select payments.*,orderdate
  200. from payments join orders
  201. on payments.orderid=orders.orderid;
  202.  
  203. --List reviews with product names and customer names
  204. select productname,name
  205. from reviews join products
  206. on reviews.productid=products.productid
  207. join customers
  208. on customers.customerid=reviews.customerid;
  209.  
  210. --List all orders with customer name, city, and order date
  211. select name,city,orderdate
  212. from orders join customers
  213. on orders.customerid=customers.customerid
  214. join addresses
  215. on addresses.customerid=customers.customerid;
  216.  
  217. --Show each order item with product name, category, and quantity
  218. select productname,categoryname,quantity
  219. from categories join products
  220. on categories.categoryid=products.categoryid
  221. join orderitems
  222. on products.productid=orderitems.orderid;
  223.  
  224. --Customers who never placed an order
  225. select customers.*
  226. from customers left join orders
  227. on customers.customerid=orders.customerid
  228. where orders.orderid is null;
  229.  
  230. --Orders with no payments
  231. select orders.*
  232. from orders left join payments
  233. on orders.orderid=payments.orderid
  234. where payments.orderid is null;
  235.  
  236. -- part 2
  237. -- Number of orders per customer
  238. select customers.customerid,customers.name ,count(*) as NumberOfOrders
  239. from orders inner join customers
  240. on orders.customerid=customers.customerid
  241. group by customers.customerid,customers.name;
  242.  
  243. --Total spending per customer
  244. select customers.customerid,sum(amount) as total_amount
  245. from orders join customers
  246. on orders.customerid =customers.customerid
  247. join payments
  248. on orders.orderid =payments.orderid
  249. group by customers.customerid
  250.  
  251. --Number of products per category
  252. select products.categoryid,count(productid) as no_of_products
  253. from products join categories
  254. on products.categoryid =categories.categoryid
  255. group by products.categoryid;
  256.  
  257.  
  258. --Average rating per product
  259. select products.productid, avg (rating) as avg_rating
  260. from products join reviews
  261. on products.productid =reviews.productid
  262. group by products.productid
  263.  
  264.  
  265. --Total quantity sold per product for those ever sold
  266. select products.productid,sum(quantity) as total
  267. from products join orderitems
  268. on products.productid =orderitems.productid
  269. join orders
  270. on orderitems.orderid =orders.orderid
  271. group by products.productid
  272.  
  273. --Total revenue per order
  274. select products.productid as product_id,count(*) as total
  275. from products join reviews
  276. on products.productid =reviews.productid
  277. group by product_id
  278.  
  279. --Customers who made more than 5 orders
  280. select customers.customerid
  281. from customers join orders
  282. on customers.customerid =orders.customerid
  283. group by customers.customerid
  284. having count(orderid)>5;
  285.  
  286. --Customers who spent more than 1000
  287.  
  288. select customers.customerid
  289. from orders join customers
  290. on orders.customerid =customers.customerid
  291. join payments
  292. on orders.orderid =payments.orderid
  293. group by customers.customerid
  294. having sum(amount)>1000;
  295.  
  296.  
  297. --Orders with total value > 500
  298. select orders.orderid
  299. from orders join payments
  300. on orders.orderid =payments.orderid
  301. group by orders.orderid
  302. having sum(amount)>500;
  303.  
  304.  
  305. --Products with price above average
  306. select productid
  307. from products
  308. where price
  309. >
  310. (
  311. select avg(price)
  312. from products)
  313.  
  314. --Most expensive product
  315. select * from products
  316. where price=(select max(price) from products);
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
1|1|1|2025-06-01|Delivered
2|2|3|2025-06-05|Pending
3|1|2|2025-06-07|Delivered
4|3|4|2025-06-10|Shipped
5|5|6|2025-06-12|Pending
3|2|iPhone 15|50000|12
4|2|Samsung S24|42000|15
1|1|Dell XPS 13|35000|10
2|1|HP Pavilion|28000|8
6|3|Mechanical Keyboard|1500|30
5|3|Wireless Mouse|500|50
Ahmed Ali|1
Mona Adel|4
5|5|6|5|Great keyboard|2025-06-19
4|1|5|4|Good mouse|2025-06-18
3|3|4|5|Amazing camera|2025-06-17
2|2|3|4|Very good phone|2025-06-16
1|1|1|5|Excellent laptop|2025-06-15
1|1|Dell XPS 13|35000|10|Laptops
2|1|HP Pavilion|28000|8|Laptops
3|2|iPhone 15|50000|12|Phones
4|2|Samsung S24|42000|15|Phones
5|3|Wireless Mouse|500|50|Accessories
6|3|Mechanical Keyboard|1500|30|Accessories
1|1|2025-06-01|36000|Credit Card|2025-06-01
2|2|2025-06-05|50000|Cash|2025-06-05
3|3|2025-06-07|29500|Credit Card|2025-06-07
4|4|2025-06-10|42000|PayPal|2025-06-10
Dell XPS 13|Ahmed Ali
iPhone 15|Sara Mohamed
Samsung S24|Omar Hassan
Wireless Mouse|Ahmed Ali
Mechanical Keyboard|Youssef Sami
Ahmed Ali|Cairo|2025-06-01
Ahmed Ali|Giza|2025-06-01
Sara Mohamed|Alexandria|2025-06-05
Ahmed Ali|Cairo|2025-06-07
Ahmed Ali|Giza|2025-06-07
Omar Hassan|Mansoura|2025-06-10
Youssef Sami|Tanta|2025-06-12
Dell XPS 13|Laptops|1
Dell XPS 13|Laptops|2
HP Pavilion|Laptops|1
iPhone 15|Phones|1
iPhone 15|Phones|1
Samsung S24|Phones|1
Wireless Mouse|Accessories|3
Wireless Mouse|Accessories|2
4|Mona Adel|mona@gmail.com|01044444444|2025-04-05
5|5|6|2025-06-12|Pending
1|Ahmed Ali|2
2|Sara Mohamed|1
3|Omar Hassan|1
5|Youssef Sami|1
1
2
3
1
2
3
4
stderr
Error: near line 1: near "schema": syntax error
Error: near line 3: near "set": syntax error
Error: near line 176: near "select": syntax error
Error: near line 244: near "select": syntax error
Error: near line 259: near "select": syntax error
Error: near line 306: near "select": syntax error