Straightforward once you see it as a two-step group-by.
First, aggregate the accounts table to count accounts per user. Then, use a CASE statement to assign each user to a bucket based on their account count. Finally, group by the bucket label and count the number of users in each bucket.
Pro tip: Consider whether to include users with 0 or 1 account; the question specifies buckets for 2, 3, and 4+, so you may need to filter them out. Also, ensure you handle NULLs appropriately in the user ID column.
Write a subquery that groups the accounts table by user_id and counts the number of accounts for each user.
In an outer query, use a CASE statement to categorize each user based on their account count into '2', '3', or '4+'.
Group the results by the bucket label and count the number of users in each bucket.
Optionally, filter out users with fewer than 2 accounts and order the results by bucket label for clarity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
This one had more moving parts than it looked.
First, identify users with at least two accounts by grouping the accounts table by user_id and filtering for counts >= 2. Then, for each such user, determine if any of their accounts has at least one unread notification (read_at IS NULL) as of the cutoff date 2025-01-01, and compute the percentage of users meeting this condition.
Pro tip: Clarify whether 'unread at that point' means the notification was created before the cutoff and still unread, or simply read_at is NULL regardless of creation date. In practice, you should consider the notification's creation timestamp to avoid counting future notifications.
Query the accounts table to find user_ids that have at least two accounts. This gives the denominator population.
From the notifications table, select notifications where read_at IS NULL and (if applicable) created_at <= '2025-01-01' to represent unread status at the cutoff.
Join the filtered notifications to the accounts table to associate each notification with its user_id.
For each multi-account user, check if they have at least one unread notification across any account. This yields the numerator.
Divide the number of multi-account users with at least one unread notification by the total number of multi-account users, then multiply by 100 to get the percentage.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.