← Practice

Top 3 spending categories: Новосибирск, mass, Jun–Nov 2025

Level: MiddleTopic: Top-N per group100 XP

Instructions

Spending is transactions (transactions) with amount < 0 and tx_time on or after June 1, 2025 and strictly before December 1, 2025, across all of the client's accounts (including the category transfer); the spending in a category is the sum of -amount. Consider clients from the city 'Новосибирск' with the segment 'mass'; clients and categories without spending in this period are not considered. For each client, rank the categories by spending descending with ROW_NUMBER(); for equal spending the category whose name comes earlier alphabetically (the smaller category value) gets the better place. Output the 3 categories with the best places for each client (if the client has fewer categories, output all of them). Columns: client_id, category, spent, 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

accounts.client_id (0…N) ↔ clients.id (1…1)transactions.account_id (0…N) ↔ accounts.id (1…1)accountsPKidintegerFKclient_idintegerclientsPKidintegertransactionsPKidintegerFKaccount_idinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneaccounts.client_id → clients.id
  • N:1Many-to-onetransactions.account_id → accounts.id

Table data

PostgreSQL

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

Dataset schema