#322 Calculating User Income and Expenses

Companies:
Sberbank
Themes:
SQLIndexesPractice

A user wants to query how much money they have spent. They also want to query how much money they have received. It should also be possible to track this by date. How can this work efficiently in a large database with millions of queries?

CREATE TABLE cash.cash_history (
    id uuid NOT NULL,
    user_id uuid NULL,
    operation_id uuid NULL,
    server_time timestamptz NULL,
    sum int8 NULL,
    CONSTRAINT cash_history_pk PRIMARY KEY (id),
    CONSTRAINT cash_history_un UNIQUE (operation_id)
)

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in