Skip to content
Quantoros

For each customer who has transactions, return their customerid and most recent transaction date (alias lasttxn).…

SQLLevel: IntermediateDates

For each customer who has transactions, return their customer_id and most recent transaction date (alias last_txn).

Table definitions (from the source exercise):

CREATE TABLE accounts(account_id INTEGER PRIMARY KEY, customer_id INTEGER, product_type TEXT, open_date TEXT, credit_limit INTEGER, status TEXT);
CREATE TABLE transactions(transaction_id INTEGER PRIMARY KEY, account_id INTEGER, transaction_date TEXT, amount REAL, merchant_category TEXT);
Hints
  • Write an interleaving of two concurrent operations.
  • Which observations does the isolation level permit, and what happens on a mid-operation failure?

Answer

Approach: Join accounts to transactions and take MAX of the date per customer.

SELECT a.customer_id, MAX(t.transaction_date) AS last_txn
FROM accounts a
JOIN transactions t ON t.account_id = a.account_id
GROUP BY a.customer_id;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions