A window function performs calculations across a set of rows related to the current row without collapsing the rows into a single output row. It lets you do things like running totals or ranking within groups.
Click to reveal answer
beginner
What does the partitionBy clause do in a window specification?
It divides the data into groups (partitions) so the window function works independently within each group, like calculating ranks separately for each department in a company.
Click to reveal answer
intermediate
Explain the difference between orderBy and partitionBy in window functions.
partitionBy groups rows into separate sets. orderBy sorts rows within each partition to define the order for calculations like running totals or ranks.
Click to reveal answer
intermediate
What is the purpose of the rowsBetween method in window functions?
It defines the frame of rows around the current row to include in the calculation, for example, the previous 2 rows and the next 2 rows, helping to calculate moving averages.
Click to reveal answer
beginner
Give an example of a common window function in Spark and its use.
The rank() function assigns a rank to each row within a partition ordered by a column. For example, ranking salespeople by sales amount within each region.
Click to reveal answer
What does a window function NOT do?
ACalculate running totals
BCalculate moving averages
CRank rows within groups
DAggregate rows into a single output row
✗ Incorrect
Window functions do calculations across rows but keep all rows in the output; they do not collapse rows into one.
Which clause in a window specification divides data into groups?
AorderBy
BpartitionBy
CrowsBetween
Dfilter
✗ Incorrect
partitionBy splits data into groups for independent window calculations.
What does orderBy do inside a window function?
AFilters rows
BRemoves duplicates
CSorts rows within partitions
DGroups rows
✗ Incorrect
orderBy sorts rows within each partition to define calculation order.
Which method defines the frame of rows around the current row?
ArowsBetween
BorderBy
CpartitionBy
Dselect
✗ Incorrect
rowsBetween sets the range of rows to include in the window frame.
What would rank() do in a window function?
AAssign a rank to rows within partitions
BCalculate sum of values
CFilter rows by rank
DCalculate average
✗ Incorrect
rank() assigns ranks to rows ordered within each partition.
Explain how window functions differ from regular aggregation functions in Spark.
Think about whether rows are combined or kept separate.
You got /4 concepts.
Describe how you would use partitionBy and orderBy together in a window function.
Consider grouping first, then sorting inside groups.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of window functions in Apache Spark?
easy
A. To perform calculations across rows related to the current row without collapsing the data
B. To group data and reduce the number of rows
C. To filter rows based on a condition
D. To join two datasets together
Solution
Step 1: Understand window function behavior
Window functions calculate values across related rows but keep all original rows intact.
Step 2: Compare with grouping
Grouping reduces rows by aggregation, but window functions add columns without reducing rows.
Final Answer:
To perform calculations across rows related to the current row without collapsing the data -> Option A
Quick Check:
Window functions add calculations without grouping ≠ A [OK]
Hint: rank() gives same rank to ties, skips next ranks [OK]
Common Mistakes:
Confusing rank() with dense_rank()
Assigning different ranks to tied values
Not ordering data before applying rank
4. Identify the error in this PySpark code snippet using window functions:
from pyspark.sql.window import Window
from pyspark.sql.functions import row_number
windowSpec = Window.partitionBy('department')
df = df.withColumn('row_num', row_number().over(windowSpec.orderBy('salary')))
medium
A. Missing import for Window functions causes error
B. windowSpec.orderBy('salary') is valid; no error in code
C. row_number() cannot be used with partitionBy
D. windowSpec.orderBy('salary') is invalid; orderBy should be called on Window, not on windowSpec
Solution
Step 1: Check windowSpec usage
windowSpec is a WindowSpec object; calling orderBy on it is valid and returns a new WindowSpec.
Step 2: Confirm chaining
Chaining orderBy on windowSpec is allowed to refine the window specification.
Final Answer:
windowSpec.orderBy('salary') is valid; no error in code -> Option B
Quick Check:
WindowSpec supports chaining orderBy to refine window [OK]
Hint: WindowSpec supports chaining orderBy to refine window [OK]
Common Mistakes:
Calling orderBy on windowSpec instead of Window
Assuming row_number can't be used with partitionBy
Forgetting to import Window or functions
5. You have a sales dataset with columns: 'store', 'date', and 'sales'. You want to add a column showing the 3-day moving average of sales per store ordered by date. Which PySpark window function setup and code will achieve this?
hard
A. Use Window.partitionBy('store').orderBy('date').rowsBetween(0, 2) and avg('sales').over(windowSpec)
B. Use Window.partitionBy('store').orderBy('date').rangeBetween(-2, 0) and avg('sales').over(windowSpec)
C. Use Window.orderBy('date').rowsBetween(-3, 0) and avg('sales').over(windowSpec)
D. Use Window.partitionBy('store').orderBy('date').rowsBetween(-2, 0) and avg('sales').over(windowSpec)
Solution
Step 1: Define window for 3-day moving average per store
Partition by 'store' to separate stores, order by 'date' for time sequence, use rowsBetween(-2, 0) for current and previous 2 rows.
Step 2: Apply avg() over this window
Use avg('sales').over(windowSpec) to compute moving average over the defined window.
Final Answer:
Use Window.partitionBy('store').orderBy('date').rowsBetween(-2, 0) and avg('sales').over(windowSpec) -> Option D
Quick Check:
rowsBetween(-2,0) for 3-day window, partition by store [OK]
Hint: Use rowsBetween(-2,0) for 3-row moving average [OK]
Common Mistakes:
Using rangeBetween instead of rowsBetween for row count
Not partitioning by store
Using wrong rowsBetween range (like 0 to 2)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.