Day – 27 :
Window Functions in
SQL
Course created by Satish Dhawale | [Link]
Window functions perform calculations across a set of
table rows related to the current row. They are useful for
Window functions ranking, calculating running totals, percentages, and much
more.
Assign a unique row
number to each product
within the same category.
SELECT
product_name,
category,
price,
ROW_NUMBER() OVER (PARTITION BY
category ORDER BY price DESC) AS
row_num
FROM products;
Calculate the running
total of prices across
all products.
SELECT
product_name,
price,
SUM(price) OVER (ORDER BY price
DESC) AS running_total
FROM products;
Ranking Window
Function
SELECT
product_name,
category,
price,
DENSE_RANK() OVER
(PARTITION BY category ORDER BY price
DESC) AS Ranking
FROM products;