-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_practice2.sql
137 lines (106 loc) · 4.38 KB
/
window_practice2.sql
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
-- Script to create the Product table and load data into it.
DROP TABLE product;
CREATE TABLE product
(
product_category varchar(255),
brand varchar(255),
product_name varchar(255),
price int
);
INSERT INTO product VALUES
('Phone', 'Apple', 'iPhone 12 Pro Max', 1300),
('Phone', 'Apple', 'iPhone 12 Pro', 1100),
('Phone', 'Apple', 'iPhone 12', 1000),
('Phone', 'Samsung', 'Galaxy Z Fold 3', 1800),
('Phone', 'Samsung', 'Galaxy Z Flip 3', 1000),
('Phone', 'Samsung', 'Galaxy Note 20', 1200),
('Phone', 'Samsung', 'Galaxy S21', 1000),
('Phone', 'OnePlus', 'OnePlus Nord', 300),
('Phone', 'OnePlus', 'OnePlus 9', 800),
('Phone', 'Google', 'Pixel 5', 600),
('Laptop', 'Apple', 'MacBook Pro 13', 2000),
('Laptop', 'Apple', 'MacBook Air', 1200),
('Laptop', 'Microsoft', 'Surface Laptop 4', 2100),
('Laptop', 'Dell', 'XPS 13', 2000),
('Laptop', 'Dell', 'XPS 15', 2300),
('Laptop', 'Dell', 'XPS 17', 2500),
('Earphone', 'Apple', 'AirPods Pro', 280),
('Earphone', 'Samsung', 'Galaxy Buds Pro', 220),
('Earphone', 'Samsung', 'Galaxy Buds Live', 170),
('Earphone', 'Sony', 'WF-1000XM4', 250),
('Headphone', 'Sony', 'WH-1000XM4', 400),
('Headphone', 'Apple', 'AirPods Max', 550),
('Headphone', 'Microsoft', 'Surface Headphones 2', 250),
('Smartwatch', 'Apple', 'Apple Watch Series 6', 1000),
('Smartwatch', 'Apple', 'Apple Watch SE', 400),
('Smartwatch', 'Samsung', 'Galaxy Watch 4', 600),
('Smartwatch', 'OnePlus', 'OnePlus Watch', 220);
COMMIT;
-- All the SQL Queries written during the video
select * from product;
-- FIRST_VALUE
-- Write query to display the most expensive product under each category (corresponding to each record)
select *,
first_value(product_name) over(partition by product_category order by price desc) as most_exp_product
from product;
-- LAST_VALUE
-- Write query to display the least expensive product under each category (corresponding to each record)
select *,
first_value(product_name)
over(partition by product_category order by price desc)
as most_exp_product,
last_value(product_name)
over(partition by product_category order by price desc
range between unbounded preceding and unbounded following)
as least_exp_product
from product
WHERE product_category ='Phone';
-- Alternate way to write SQL query using Window functions
select *,
first_value(product_name) over w as most_exp_product,
last_value(product_name) over w as least_exp_product
from product
WHERE product_category ='Phone'
window w as (partition by product_category order by price desc
range between unbounded preceding and unbounded following);
-- NTH_VALUE
-- Write query to display the Second most expensive product under each category.
select *,
first_value(product_name) over w as most_exp_product,
last_value(product_name) over w as least_exp_product,
nth_value(product_name, 5) over w as second_most_exp_product
from product
window w as (partition by product_category order by price desc
range between unbounded preceding and unbounded following);
-- NTILE
-- Write a query to segregate all the expensive phones, mid range phones and the cheaper phones.
select x.product_name,
case when x.buckets = 1 then 'Expensive Phones'
when x.buckets = 2 then 'Mid Range Phones'
when x.buckets = 3 then 'Cheaper Phones' END as Phone_Category
from (
select *,
ntile(3) over (order by price desc) as buckets
from product
where product_category = 'Phone') x;
-- CUME_DIST (cumulative distribution) ;
/* Formula = Current Row no (or Row No with value same as current row) / Total no of rows */
-- Query to fetch all products which are constituting the first 30%
-- of the data in products table based on price.
select product_name, cume_dist_percetage
from (
select *,
cume_dist() over (order by price desc) as cume_distribution,
round(cume_dist() over (order by price desc)::numeric * 100,2)||'%' as cume_dist_percetage
from product) x
where x.cume_distribution <= 0.3;
-- PERCENT_RANK (relative rank of the current row / Percentage Ranking)
/* Formula = Current Row No - 1 / Total no of rows - 1 */
-- Query to identify how much percentage more expensive is "Galaxy Z Fold 3" when compared to all products.
select product_name, per
from (
select *,
percent_rank() over(order by price) ,
round(percent_rank() over(order by price)::numeric * 100, 2) as per
from product) x
where x.product_name='Galaxy Z Fold 3';