Window Functions in SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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