0
0
SQLquery~5 mins

Percent of total with window functions in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a window function in SQL?
A window function performs a calculation across a set of table rows related to the current row without collapsing the rows into a single output row.
Click to reveal answer
beginner
How do you calculate the percent of total using window functions?
Divide the value of each row by the sum of all values over the entire set using SUM() as a window function, then multiply by 100.
Click to reveal answer
beginner
What does the OVER() clause do in a window function?
It defines the window or set of rows the function should operate on, such as all rows or a partition of rows.
Click to reveal answer
intermediate
Write a simple SQL snippet to calculate the percent of total sales per product.
SELECT product, sales, 100.0 * sales / SUM(sales) OVER() AS percent_of_total FROM sales_table;
Click to reveal answer
intermediate
Why use window functions instead of GROUP BY for percent of total?
Window functions keep all rows visible and add the percent calculation as a new column, while GROUP BY aggregates rows and reduces detail.
Click to reveal answer
Which SQL clause is used to define the set of rows for a window function?
AOVER()
BGROUP BY
CWHERE
DHAVING
To calculate percent of total sales per row, which function is typically used inside OVER()?
ACOUNT()
BMAX()
CAVG()
DSUM()
What does multiplying by 100 do in a percent of total calculation?
AConverts fraction to percentage
BRounds the number
CFilters rows
DAggregates data
Which of these is true about window functions?
AThey only work with GROUP BY
BThey collapse rows into one
CThey add calculations without collapsing rows
DThey filter rows
If you want percent of total sales per category, what should you add to the window function?
AGROUP BY category
BPARTITION BY category
CORDER BY category
DWHERE category
Explain how to calculate the percent of total for each row using SQL window functions.
Think about how to get total sum without grouping rows.
You got /4 concepts.
    Describe the difference between using GROUP BY and window functions for percent of total calculations.
    Consider what happens to rows in each case.
    You got /4 concepts.