GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 22(b)

Find minimum historical account balances

Database Management Systems · SQL & Queries · Full GATE CSE 2000 paper

GATE 2000, Q22(b) Descriptive
Report issue

SQL & Queries

Question 22(b), GATE 2000: Bank relation transaction(transno,acctno,date,amount) stores positive deposits and negative withdrawals. TP must contain (acctno,T1.date,T2.amount) for every pair of transactions on the same account with T2.date<=T1.date. Using only TP, create intermediate view V and query each account’s minimum balance after a transaction, excluding the initial zero. At most one transaction per account per day; each account has at least one transaction.
Question 22(b), GATE 2000: Bank relation transaction(transno,acctno,date,amount) stores positive deposits and negative withdrawals. TP must contain (acctno,T1.date,T2.amount) for every pair of transactions on the same account with T2.date<=T1.date. Using only TP, create intermediate view V and query each account’s minimum balance after a transaction, excluding the initial zero. At most one transaction per account per day; each account has at least one transaction.
Question 22(b), GATE 2000: Bank relation transaction(transno,acctno,date,amount) stores positive deposits and negative withdrawals. TP must contain (acctno,T1.date,T2.amount) for every pair of transactions on the same account with T2.date<=T1.date. Using only TP, create intermediate view V and query each account’s minimum balance after a transaction, excluding the initial zero. At most one transaction per account per day; each account has at least one transaction.
Tap to enlarge
Answer & solution

Answer: V sums amounts by account and observation date; final query takes MIN(balance) by account. (no official key was published)

Use two aggregation levels:
CREATE VIEW V (acctno, txn_date, balance) AS
SELECT acctno, txn_date, SUM(amount)
FROM TP
GROUP BY acctno, txn_date;

SELECT acctno, MIN(balance) AS minbalance
FROM V
GROUP BY acctno;
For a fixed account and T1 date, TP has one amount row for every transaction up to and including that date. SUM therefore gives the cumulative balance immediately after the transaction, assuming the opening balance is zero as specified. The one-transaction-per-account-per-day condition ensures each observation date has one T1 row, avoiding repeated copies of the entire prefix caused by multiple T1 transactions on the same day. Taking MIN over these cumulative balances returns the lowest post-transaction balance, including negative balances when appropriate. There is no row for account creation, so the initial zero is not accidentally included; a positive-only account correctly returns its first positive balance rather than zero. An account with amounts 100,100,−250 has balances 100,200,−50 and minimum−50. DISTINCT in TP would collapse the two 100 entries at the later dates and corrupt these sums. Distinct dates, not distinct amounts, define observation groups. Non-null numeric amounts and a comparable chronological date type are implicit in the bank model.

Original paper, page 10