← Practice

Accounts: 10+ expenses in the category transport

Questions like this come up in interviews at these companies: Самокат, Циан, Ozon

Level: JuniorTopic: Aggregates, GROUP BY and HAVING50 XP

Instructions

Among the transactions of the category 'transport' with a negative amount of at least 1500 in absolute value (amount at most -1500), compute by account (account_id): tx_cnt, the number of such transactions, and avg_spent, the average expense as a positive number (minus the average amount), rounded to 2 decimal places. Select account_id, tx_cnt, avg_spent; keep the accounts where tx_cnt is at least 10. 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