GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 22(a)

Define transaction-prefix SQL view

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

GATE 2000, Q22(a) Descriptive
Report issue

SQL & Queries

Question 22(a), 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. Define TP in SQL.
Question 22(a), 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. Define TP in SQL.
Tap to enlarge
Answer & solution

Answer: Self-join by equal account and T2.date<=T1.date, without DISTINCT. (no official key was published)

One SQL definition is

CREATE VIEW TP (acctno, txn_date, amount) AS
SELECT T1.acctno, T1.date, T2.amount
FROM transaction AS T1
JOIN transaction AS T2
  ON T1.acctno = T2.acctno
 AND T2.date <= T1.date;

Here T1 chooses a balance-observation date and T2 chooses one transaction contributing to that balance. Include equality so the transaction on the observation date contributes; using < would produce the balance before, rather than after, that transaction and omit the first date’s self-pair. Each qualifying transaction pair contributes a row. Do not add DISTINCT: SQL’s bag semantics deliberately preserve separate same-amount transactions even though the projected triples may be identical. For example two deposits of 100 on different days generate two copies of (account,later_date,100), both of which must be summed. transaction and date can be reserved identifiers in some SQL dialects; quote the existing identifiers with that dialect’s supported delimiters if needed.

Original paper, page 10