← Practice

DENSE_RANK: top 2 channels by buyers, web, since May 2025

Level: MiddleTopic: Top-N per group100 XP

Instructions

Consider users with the platform 'web' whose referrer is not NULL, and their purchases made on or after May 1, 2025. For each pair (country users.country, channel users.referrer) compute the metric buyers: the number of distinct users with this referrer who have purchases; pairs without such purchases are not considered. Within a country, rank the channels by the metric descending using DENSE_RANK(): channels with an equal metric get the same place, and the next place is not skipped. Output the channels with place <= 2; because of ties there may be more than 2 of them in a country. Columns: country, referrer, buyers, place. Row order does not matter.

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

events.user_id (0…N) ↔ users.id (1…1)purchases.user_id (0…N) ↔ users.id (1…1)eventsPKidintegerFKuser_idintegerpurchasesPKidintegerFKuser_idintegerusersPKidinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneevents.user_id → users.id
  • N:1Many-to-onepurchases.user_id → users.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema