Skip to content
Quantoros

Return the transactionid and amount of every transaction larger than the overall average amount. Table definitions…

SQLLevel: IntermediateSubqueries and CTEs

Return the transaction_id and amount of every transaction larger than the overall average amount.

Table definitions (from the source exercise):

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: A scalar subquery in the WHERE clause.

SELECT transaction_id, amount
FROM transactions
WHERE amount > (SELECT AVG(amount) FROM transactions);

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