Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
CREATE TABLE accounts (
account_id SERIAL PRIMARY KEY,
account_holder VARCHAR(100),
account_type VARCHAR(50)
);
CREATE TABLE transactions (
transaction_id SERIAL PRIMARY KEY,
account_id INTEGER REFERENCES accounts(account_id),
amount DECIMAL(12,2),
transaction_type VARCHAR(20),
transaction_date DATE
);
INSERT INTO accounts (account_holder, account_type) VALUES
('Alice Johnson', 'Savings'),
('Bob Smith', 'Checking');
INSERT INTO transactions (
account_id,
amount,
transaction_type,
transaction_date
) VALUES
(1, 1500, 'deposit', '2025-01-01'),
(1, 300, 'withdrawal', '2025-01-10'),
(2, 2200, 'deposit', '2025-01-03'),
(2, 500, 'withdrawal', '2025-01-15');
SELECT
account_id,
SUM(
CASE
WHEN transaction_type = 'deposit' THEN amount
ELSE -amount
END
) AS current_balance
FROM transactions
GROUP BY account_id;
SELECT
transaction_date,
amount,
SUM(amount) OVER (
ORDER BY transaction_date
) AS running_total
FROM transactions;Coding works best on desktop or with an external keyboard.