← Practice

Spending above income: vip, RUB, 4 mo.: Mar–Jun 2025

Level: MiddleTopic: CTE100 XP

Instructions

Consider transactions on accounts in the currency RUB (accounts.currency) with tx_time on or after March 1, 2025 and strictly before July 1, 2025. A client's income is the sum of transactions with amount > 0, and spending is the sum of the absolute values of transactions with amount < 0 (that is, -sum(amount)); all such accounts of the client are taken. Find clients of the segment 'vip' who had both income and spending, and whose spending is strictly greater than their income. Output id, name, income and spend. 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