A company wants to identify the most engaged users of its platform. Using activity recorded during Q2 2025, write a PostgreSQL query that ranks every user and returns the top 10% based on a defined engagement score.
Requirements
- Include every user, including users with no qualifying activity during Q2 2025.
- Calculate quarterly sessions and completed tasks from
engagement_events.
- Define
engagement_score as (sessions * 2) + completed_tasks.
- Use a window function to divide users into ten engagement deciles and return only decile 1, ordered by score descending.
Output details
- Return columns in this order:
user_id, full_name, sessions, completed_tasks, engagement_score, engagement_decile.
- Treat Q2 2025 as dates on or after
DATE '2025-04-01' and before DATE '2025-07-01'.
- Represent users without qualifying activity with
0 for sessions and completed_tasks.
- Break equal
engagement_score values by user_id ascending, including in the final ordering.
- Assign users to ten equal row-count deciles, giving any remainder rows to earlier deciles.