-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_query.sql
More file actions
104 lines (88 loc) · 2.68 KB
/
sql_query.sql
File metadata and controls
104 lines (88 loc) · 2.68 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
SELECT * FROM jewelry_sales;
-- a. SELECT, WHERE, ORDER BY, GROUP BY
-- Total purchases by category, ordered by count
SELECT category, COUNT(*) AS total_purchases
FROM jewelry_sales
WHERE is_purchase = 1
GROUP BY category
ORDER BY total_purchases DESC;
-- CREATE table users
CREATE TABLE users (
user_id VARCHAR(50) PRIMARY KEY,
user_name VARCHAR(100),
user_email VARCHAR(100)
);
-- insert values in users
INSERT INTO users (user_id, user_name, user_email) VALUES
('U001', 'Alice Smith', 'alice@example.com'),
('U002', 'Bob Johnson', 'bob@example.com'),
('U003', 'Charlie Lee', 'charlie@example.com');
-- CREATE table products
CREATE TABLE products (
product_id VARCHAR(50) PRIMARY KEY,
product_name VARCHAR(100),
brand VARCHAR(50)
);
-- insert values in products
INSERT INTO products (product_id, product_name, brand) VALUES
('P001', 'Diamond Ring', 'Tiffany'),
('P002', 'Gold Necklace', 'Cartier'),
('P003', 'Silver Bracelet', 'Pandora');
-- b. joins
-- inner join
SELECT js.user_id, u.user_name, js.category, js.price
FROM jewelry_sales js
INNER JOIN users u ON js.user_id = u.user_id
WHERE js.is_purchase = 1;
-- left join
SELECT js.product_id, js.category, js.price, p.product_name, p.brand
FROM jewelry_sales js
LEFT JOIN products p ON js.product_id = p.product_id;
-- right join
SELECT p.product_name, js.price, js.category
FROM products p
RIGHT JOIN jewelry_sales js ON js.product_id = p.product_id;
-- c. Subquery: Find all purchases above the average price
SELECT *
FROM jewelry_sales
WHERE price > (
SELECT AVG(price)
FROM jewelry_sales
WHERE is_purchase = 1
)
AND is_purchase = 1;
-- d. Aggregate Functions (SUM, AVG)
-- Avg price by gem
SELECT gem, AVG(price) AS avg_price
FROM jewelry_sales
WHERE gem IS NOT NULL
GROUP BY gem;
-- Total revenue from purchases
SELECT SUM(price * quantity) AS total_revenue
FROM jewelry_sales
WHERE is_purchase = 1;
-- Count total items sold per material
SELECT material, SUM(quantity) AS total_items_sold
FROM jewelry_sales
GROUP BY material
ORDER BY total_items_sold DESC;
-- e. Create Views for analysis
-- View for category-wise sales summary
CREATE VIEW category_sales_summary AS
SELECT category, COUNT(*) AS total_sales, AVG(price) AS avg_price
FROM jewelry_sales
WHERE is_purchase = 1
GROUP BY category;
-- View for gem popularity
CREATE VIEW gem_popularity AS
SELECT gem, COUNT(*) AS gem_count
FROM jewelry_sales
GROUP BY gem
ORDER BY gem_count DESC;
-- f. Optimize with Indexes
-- Index on user_id
CREATE INDEX idx_user_id ON jewelry_sales(user_id);
-- Index on product_id for JOIN performance
CREATE INDEX idx_product_id ON jewelry_sales(product_id);
-- Index on category for filtering
CREATE INDEX idx_category ON jewelry_sales(category);