Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

SQL Script Window Functions-Part 2

The document contains SQL scripts for creating a 'Product' table, inserting various products with their categories, brands, names, and prices. It also includes several SQL queries utilizing window functions to analyze product pricing, such as finding the most and least expensive products, categorizing phones by price range, and calculating cumulative distribution and percentage rank. The queries demonstrate advanced SQL techniques for data analysis within the product dataset.

Uploaded by

shanumehta007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Script Window Functions-Part 2

The document contains SQL scripts for creating a 'Product' table, inserting various products with their categories, brands, names, and prices. It also includes several SQL queries utilizing window functions to analyze product pricing, such as finding the most and least expensive products, categorizing phones by price range, and calculating cumulative distribution and percentage rank. The queries demonstrate advanced SQL techniques for data analysis within the product dataset.

Uploaded by

shanumehta007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

-- 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';

You might also like