0% found this document useful (0 votes)
55 views

Window Functions in SQL

Window functions allow calculations across rows related to the current row without a GROUP BY clause, preserving the original data structure. They are used to calculate running totals, moving averages, cumulative sums, and more. Window functions have five main clauses: the window function, expression, PARTITION BY, ORDER BY, and ROWS frame specification. An example query calculates the running total revenue for each product ordered by date using a window function without changing the original data.

Uploaded by

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

Window Functions in SQL

Window functions allow calculations across rows related to the current row without a GROUP BY clause, preserving the original data structure. They are used to calculate running totals, moving averages, cumulative sums, and more. Window functions have five main clauses: the window function, expression, PARTITION BY, ORDER BY, and ROWS frame specification. An example query calculates the running total revenue for each product ordered by date using a window function without changing the original data.

Uploaded by

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

Window Functions allow you to perform

calculations across a set of rows that are


related to the current row, without the need
for a GROUP BY clause. This means you can
calculate running totals, moving averages,
cumulative sums, and much more, all while
preserving the original data structure.
1. <window_function>: This is the actual window function you
want to use, such as ROW_NUMBER(), RANK(),
DENSE_RANK(), NTILE(), SUM(), AVG(), MIN(), MAX(), etc.
2. <expression>: The column or expression on which you want
to perform the window function.
3. PARTITION BY: This clause is optional and is used to divide
the result set into partitions to which the window function will
be applied.
4. ORDER BY: This clause is also optional and is used to specify
the order in which the rows in the result set are ranked.
5. ROWS <frame_specification>: This clause is also optional
and is used to define the range of rows within a partition that
the window function will operate on.
Suppose we have a table called 'sales'
with the following data:
Let's use the example Window Function
query to calculate the running total of
revenue for each product, ordered by
date:
After applying the Window Function, the
resulting table looks like this:

The running_total column shows the


cumulative sum of the revenue for each
product_id, ordered by date, without
changing the original data structure.
50. What is the most
important skill or quality
you bring to a data
analysis role?

Answer this based on your personal


strengths, such as technical expertise,
communication skills, problem-solving
abilities,

You might also like