Understanding Rolling Averages
What is the main purpose of calculating a rolling average in a time-series dataset?
- To examine data trends within a specific recent window of time
- To calculate the total sum of all records
- To select unique values from the data
- To remove duplicate records from results
- To randomly sample from the dataset
Window Function Syntax
Which of the following SQL window function clauses accurately defines a 3-day rolling window including the current row?
- ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
- ROWS BETWEEN CURRENT ROW AND 2 PRECEDING
- ROWS BETWEEN 3 PRECEDING AND CURRENT ROW
- ROWS BETWEEN CURRENT ROW AND 3 PRECEDING
- ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
Selecting Columns for Output
If you want to output each user's ID, the tweet date, and their 3-day rolling tweet average, which of the following column lists is correct?
- user_id, tweet_date, rolling_average
- username, date, tweet_count
- user_id, tweet_avg, tweet_date
- userid, date, rolling_mean
- user, tweet_date, average
Achieving Partitioned Calculations
To compute rolling averages for each user separately, which clause is essential in your window function?
- PARTITION BY user_id
- GROUP BY tweet_date
- ORDER BY tweet_count
- HAVING user_id IS NOT NULL
- DISTINCT ON user_id
Ordering for Correct Rolling Results
When defining a rolling window in SQL, what is the correct column to ORDER BY in the window function?
- The date or timestamp column
- The user ID column
- The tweet count column
- The rolling average column
- No ordering is needed
Syntax Check: AVG with Window
Which is the correct way to use AVG in a window function to calculate a rolling mean over the last 3 rows?
- AVG(tweet_count) OVER (PARTITION BY user_id ORDER BY tweet_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
- SUM(tweet_count) PARTITION BY user_id ORDER tweet_date RANGE 2 PRECEDING AND CURRENT
- AVG(tweet_count) FROM (GROUP BY user_id)
- COUNT(tweet_count) WINDOW user_id ORDER BY tweet_date ROWS BETWEEN 2 PRECEDING
- AVG tweet_count OVER (ORDER BY tweet_date GROUP BY user_id)
Rounding Averaged Results
What SQL function can you use to round the rolling average to 2 decimal places?
- ROUND(average_value, 2)
- TRUNCATE(average_value, 2)
- ROUNDUP(average_value, 2)
- FORMAT(2, average_value)
- FLOOR(average_value, 2)
Clarifying Terminology
What is another commonly used term for 'rolling average' in data analysis?
- Moving average
- Central tendency
- Aggregate mean
- Grouped mean
- Relative average
Error Spotting: Typos in SQL
Identify the typo in this SQL clause: 'ROWS BTWEEN 2 PRECEDING AND CURRENT ROW'
- 'BTWEEN' should be 'BETWEEN'
- 'CURRENT ROW' should be 'CURRENT'
- 'PRECEDING' should be 'PRECEEDING'
- 'ROWS' should be 'ROWES'
- 'AND' should be 'ADD'
Aggregating for Each User
Why is it necessary to use PARTITION BY when calculating a rolling average for every user in a tweet table?
- So each user's records are treated as separate groups during calculation
- To join two tables by user name
- To improve query performance
- To limit the results to a single user
- To remove duplicate users from output