Repository navigation
Expand file tree
/
Copy pathpractice.sql
More file actions
89 lines (80 loc) · 5.22 KB
/
Copy pathpractice.sql
File metadata and controls
89 lines (80 loc) · 5.22 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
-- PostgreSQL 12+ compatible master practice script.
-- Run in pgAdmin Query Tool or psql while connected to any database.
-- Uses a dedicated schema so the complete script runs in one execution.
-- WARNING: Re-running resets the six practice tables in this schema.
CREATE SCHEMA IF NOT EXISTS retail_store_practice;
SET search_path TO retail_store_practice, public;
DROP TABLE IF EXISTS order_items CASCADE;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (customer_id INT PRIMARY KEY, customer_name VARCHAR(100) NOT NULL, email VARCHAR(120) UNIQUE, city VARCHAR(60), signup_date DATE);
CREATE TABLE categories (category_id INT PRIMARY KEY, category_name VARCHAR(60) NOT NULL UNIQUE);
CREATE TABLE employees (employee_id INT PRIMARY KEY, employee_name VARCHAR(100) NOT NULL, department VARCHAR(60), salary DECIMAL(10,2), manager_id INT NULL, FOREIGN KEY (manager_id) REFERENCES employees(employee_id));
CREATE TABLE products (product_id INT PRIMARY KEY, product_name VARCHAR(100) NOT NULL, category_id INT, price DECIMAL(10,2) NOT NULL, stock INT NOT NULL, FOREIGN KEY (category_id) REFERENCES categories(category_id));
CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT NOT NULL, employee_id INT NULL, order_date DATE NOT NULL, status VARCHAR(20) NOT NULL, FOREIGN KEY (customer_id) REFERENCES customers(customer_id), FOREIGN KEY (employee_id) REFERENCES employees(employee_id));
CREATE TABLE order_items (order_item_id INT PRIMARY KEY, order_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, unit_price DECIMAL(10,2) NOT NULL, FOREIGN KEY (order_id) REFERENCES orders(order_id), FOREIGN KEY (product_id) REFERENCES products(product_id));
INSERT INTO customers VALUES
(1,'Aarav Sharma','aarav@example.com','Mumbai','2025-01-10'),
(2,'Diya Patel','diya@example.com','Pune','2025-02-14'),
(3,'Kabir Khan','kabir@example.com','Delhi','2025-03-01'),
(4,'Ananya Rao','ananya@example.com','Bengaluru','2025-03-18'),
(5,'Rohan Mehta','rohan@example.com','Mumbai','2025-04-02'),
(6,'Meera Iyer','meera@example.com','Chennai','2025-04-21'),
(7,'Ishaan Gupta','ishaan@example.com','Delhi','2025-05-09'),
(8,'Sara Fernandes','sara@example.com','Goa','2025-05-24'),
(9,'Neha Joshi','neha@example.com','Pune','2025-06-12'),
(10,'Vikram Singh','vikram@example.com','Jaipur','2025-07-05'),
(11,'Priya Nair','priya@example.com','Kochi','2025-08-15'),
(12,'Aditya Bose','aditya@example.com','Kolkata','2025-09-20');
INSERT INTO categories VALUES (1,'Electronics'),(2,'Accessories'),(3,'Home'),(4,'Books'),(5,'Fitness'),(6,'Stationery');
INSERT INTO employees VALUES
(1,'Kavita Desai','Sales',85000,NULL),
(2,'Arjun Kapoor','Operations',90000,NULL),
(3,'Pooja Shah','Sales',52000,1),
(4,'Rahul Verma','Sales',48000,1),
(5,'Sneha Kulkarni','Operations',55000,2),
(6,'Manish Jain','Support',42000,2);
INSERT INTO products VALUES
(1,'Wireless Mouse',2,799,60),(2,'Mechanical Keyboard',2,3499,25),
(3,'USB-C Cable',2,399,120),(4,'Laptop Stand',2,1299,40),
(5,'Bluetooth Speaker',1,2499,35),(6,'Noise Cancelling Headphones',1,6999,18),
(7,'Smart Watch',1,4999,22),(8,'Desk Lamp',3,1499,30),
(9,'Water Bottle',5,599,80),(10,'Yoga Mat',5,999,45),
(11,'SQL Basics Book',4,649,55),(12,'Python Guide',4,899,38),
(13,'Notebook Set',6,299,100),(14,'Pen Pack',6,199,150),
(15,'Office Chair',3,8999,12),(16,'Monitor 24 Inch',1,10999,14),
(17,'Webcam HD',1,2199,0),(18,'Resistance Bands',5,749,65);
INSERT INTO orders VALUES
(1001,1,3,'2025-09-01','Delivered'),(1002,2,4,'2025-09-03','Delivered'),
(1003,3,3,'2025-09-05','Delivered'),(1004,1,4,'2025-09-08','Cancelled'),
(1005,4,3,'2025-09-12','Delivered'),(1006,5,4,'2025-09-15','Delivered'),
(1007,6,3,'2025-09-19','Shipped'),(1008,2,4,'2025-09-21','Delivered'),
(1009,7,3,'2025-10-01','Delivered'),(1010,8,4,'2025-10-04','Cancelled'),
(1011,9,3,'2025-10-07','Delivered'),(1012,10,4,'2025-10-09','Shipped'),
(1013,3,3,'2025-10-12','Delivered'),(1014,5,4,'2025-10-14','Delivered'),
(1015,1,3,'2025-10-17','Pending'),(1016,11,4,'2025-10-20','Delivered'),
(1017,4,3,'2025-10-22','Pending'),(1018,9,4,'2025-10-25','Delivered');
INSERT INTO order_items VALUES
(1,1001,1,2,799),(2,1001,3,1,399),(3,1002,5,1,2499),
(4,1002,9,2,599),(5,1003,11,1,649),(6,1003,12,1,899),
(7,1004,7,1,4999),(8,1005,2,1,3499),(9,1005,4,1,1299),
(10,1006,8,2,1499),(11,1006,13,3,299),(12,1007,10,2,999),
(13,1007,9,1,599),(14,1008,6,1,6999),(15,1008,3,2,399),
(16,1009,14,5,199),(17,1009,13,2,299),(18,1010,15,1,8999),
(19,1011,16,1,10999),(20,1011,4,1,1299),(21,1012,18,2,749),
(22,1012,9,2,599),(23,1013,1,1,799),(24,1013,11,2,649),
(25,1014,5,2,2499),(26,1014,3,3,399),(27,1015,7,1,4999),
(28,1015,2,1,3499),(29,1016,12,1,899),(30,1016,13,4,299),
(31,1017,15,1,8999),(32,1018,10,1,999),(33,1018,18,2,749);
-- Start exploring:
SELECT * FROM customers;
SELECT * FROM products WHERE price > 2000 ORDER BY price DESC;
SELECT city, COUNT(*) AS customer_count FROM customers GROUP BY city;
SELECT o.order_id, c.customer_name, o.order_date, o.status
FROM orders o JOIN customers c ON o.customer_id = c.customer_id;
SELECT o.order_id, SUM(oi.quantity * oi.unit_price) AS order_total
FROM orders o JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY o.order_id ORDER BY order_total DESC;