Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Window Functions for Advanced Reporting | Advanced SQL for Data Analysis
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL for Data Analysis

bookWindow Functions for Advanced Reporting

Window functions are a powerful feature in SQL that allow you to perform calculations across sets of rows that are related to the current row, without collapsing the result set. While aggregate functions like SUM() or AVG() return a single value for a group, window functions add analytic capabilities by letting you compute things like running totals, rankings, and moving averages while still displaying each row. This is essential for advanced analytics, such as ranking your top customers, calculating moving averages of sales, or comparing each row to the overall average.

With window functions, you can answer questions that would be difficult or inefficient with only standard aggregations. For example, suppose you want to know not just the total amount each customer has spent, but also their rank among all customers based on quantity purchased. Or, you might want to see the cumulative total sales per day, so you can spot trends over time. Window functions make these types of analytics straightforward and efficient.

12345678910111213
SELECT c.customer_id, c.first_name, c.last_name, SUM(s.quantity) AS total_quantity, ROW_NUMBER() OVER (ORDER BY SUM(s.quantity) DESC) AS quantity_rank FROM customers c JOIN sales s ON c.customer_id = s.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ORDER BY quantity_rank;
copy

In this query, the ROW_NUMBER() function assigns a unique rank to each customer based on their total quantity purchased. The OVER (ORDER BY SUM(s.quantity) DESC) clause defines how the ranking is calculated—customers with higher total quantities get lower rank numbers. This is incredibly useful for creating leaderboards or identifying top performers.

Window functions often use two key clauses: PARTITION BY and ORDER BY. PARTITION BY lets you divide the result set into groups, or partitions, and then apply the window function within each group. For instance, you could rank customers within each country, or calculate running totals for each product separately. ORDER BY inside the window function specifies the order in which the function processes rows within each partition, which is essential for calculations like running totals or rankings.

Suppose you want to see the running total of sales for each day. You can use the SUM() function as a window function, ordering by date to ensure the totals accumulate correctly. If you want running totals for each product, you add a PARTITION BY product_id clause.

12345678910
SELECT sale_date, SUM(total_amount) AS daily_total, SUM(SUM(total_amount)) OVER (ORDER BY sale_date) AS running_total FROM sales GROUP BY sale_date ORDER BY sale_date;
copy

1. Which of the following are window functions in SQL?

2. What does PARTITION BY do in a window function?

3. Fill in the blanks to rank products by total sales using a window function:

question mark

Which of the following are window functions in SQL?

Select the correct answer

question mark

What does PARTITION BY do in a window function?

Select the correct answer

question-icon

Fill in the blanks to rank products by total sales using a window function:

SELECT product_id, SUM(total_amount) AS total_sales, OVER (ORDER BY SUM(total_amount) DESC) AS sales_rank FROM sales GROUP BY product_id;
The query will return each product's ID, its total sales, and its rank based on total sales, with the highest-selling product ranked first.

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain the difference between ROW_NUMBER() and RANK() in window functions?

How does PARTITION BY affect the results of a window function?

Can you show an example of using a window function to calculate a moving average?

bookWindow Functions for Advanced Reporting

Stryg for at vise menuen

Window functions are a powerful feature in SQL that allow you to perform calculations across sets of rows that are related to the current row, without collapsing the result set. While aggregate functions like SUM() or AVG() return a single value for a group, window functions add analytic capabilities by letting you compute things like running totals, rankings, and moving averages while still displaying each row. This is essential for advanced analytics, such as ranking your top customers, calculating moving averages of sales, or comparing each row to the overall average.

With window functions, you can answer questions that would be difficult or inefficient with only standard aggregations. For example, suppose you want to know not just the total amount each customer has spent, but also their rank among all customers based on quantity purchased. Or, you might want to see the cumulative total sales per day, so you can spot trends over time. Window functions make these types of analytics straightforward and efficient.

12345678910111213
SELECT c.customer_id, c.first_name, c.last_name, SUM(s.quantity) AS total_quantity, ROW_NUMBER() OVER (ORDER BY SUM(s.quantity) DESC) AS quantity_rank FROM customers c JOIN sales s ON c.customer_id = s.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ORDER BY quantity_rank;
copy

In this query, the ROW_NUMBER() function assigns a unique rank to each customer based on their total quantity purchased. The OVER (ORDER BY SUM(s.quantity) DESC) clause defines how the ranking is calculated—customers with higher total quantities get lower rank numbers. This is incredibly useful for creating leaderboards or identifying top performers.

Window functions often use two key clauses: PARTITION BY and ORDER BY. PARTITION BY lets you divide the result set into groups, or partitions, and then apply the window function within each group. For instance, you could rank customers within each country, or calculate running totals for each product separately. ORDER BY inside the window function specifies the order in which the function processes rows within each partition, which is essential for calculations like running totals or rankings.

Suppose you want to see the running total of sales for each day. You can use the SUM() function as a window function, ordering by date to ensure the totals accumulate correctly. If you want running totals for each product, you add a PARTITION BY product_id clause.

12345678910
SELECT sale_date, SUM(total_amount) AS daily_total, SUM(SUM(total_amount)) OVER (ORDER BY sale_date) AS running_total FROM sales GROUP BY sale_date ORDER BY sale_date;
copy

1. Which of the following are window functions in SQL?

2. What does PARTITION BY do in a window function?

3. Fill in the blanks to rank products by total sales using a window function:

question mark

Which of the following are window functions in SQL?

Select the correct answer

question mark

What does PARTITION BY do in a window function?

Select the correct answer

question-icon

Fill in the blanks to rank products by total sales using a window function:

SELECT product_id, SUM(total_amount) AS total_sales, OVER (ORDER BY SUM(total_amount) DESC) AS sales_rank FROM sales GROUP BY product_id;
The query will return each product's ID, its total sales, and its rank based on total sales, with the highest-selling product ranked first.

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
some-alt