Streamhub's growth team wants a quarterly reporting scaffold restricted to pro-tier users — one row per pro user per quarter.
Write a query to return the user ID and period name for every pro-user-quarter combination.
Assumptions:
- The
userstable contains every account on the platform; pro-tier users are identified byplan = 'pro'. - The
periodstable contains the calendar windows used for reporting. - The user-side condition narrows the result to pro users before they are expanded across periods.
Output:
- One row per pro-user-quarter combination, with columns
user_idandperiod_name.
Schema · analytics 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
u.id AS user_id,
p.name AS period_name
FROM
users u
CROSS JOIN periods p
WHERE
u.plan = 'pro' The shape
WHERE u.plan = 'pro' narrows the users side to pro-tier accounts; CROSS JOIN periods p then expands each surviving user across all four quarters. The result is a pro-cohort × quarter grid — exactly the scaffold the growth team's report builds on.
Clause by clause
FROM users u CROSS JOIN periods pproduces the full user-by-period cartesian product first. Conceptually, every user is paired with every quarter.WHERE u.plan = 'pro'then keeps only the rows whose user is on the pro plan. Because the filter targets a property of the left side, it effectively trimsusersdown to the pro cohort before that cohort gets paired with periods. PostgreSQL is free to apply the filter early — it doesn't have to materialise the full cross-product and then throw rows away — but the result is the same either way: pro users × every quarter.u.id AS user_idreads the pro user's ID from each paired row.p.name AS period_namereads the quarter label (Q1–Q4) from the periods side.
Why filter on users and not on the joined result
Filtering on u.plan is equivalent to writing the cohort first and then crossing it with periods — both shapes return the same rows. What the WHERE clause cannot do here is filter on a property of the combination (e.g., "only pro users in Q1"), because the cross-product treats every pairing as equally valid by construction. The filter narrows one side of the grid; it doesn't pick specific cells out of it. For "pro users across every quarter," that's the right shape — every surviving pro user gets all four periods.
You practiced restricting one side of a CROSS JOIN with a WHERE condition. The recurring shape any time a cohort × catalogue grid is the foundation of a per-segment report.