cswork / libraryOpen practice workspace

Coding questions · Others

Compute per-coin quarterly amounts and totals

This question evaluates competence in SQL data manipulation—specifically time-based conditional aggregation, grouping, joins, and handling entities with no activity—to compute per-coin quarterly sums and year-to-date totals.

Problem (SQL: Conditional Aggregation)

You have two tables:

coins

  • coin_id INT PRIMARY KEY
  • name VARCHAR

transactions

  • id BIGINT PRIMARY KEY
  • coin_id INT NOT NULL
    • Foreign key referencing coins.coin_id
  • dt DATETIME (assume UTC)
  • amount DECIMAL(18,2)

Each row in transactions is a single transaction for a coin.

Task

For a given calendar year (use a parameter :year), output one row per coin with:

  • name
  • q1_amount: sum of amount for transactions in Q1 (Jan–Mar) of :year
  • q2_amount: sum of amount for Q2 (Apr–Jun) of :year
  • q3_amount: sum of amount for Q3 (Jul–Sep) of :year
  • q4_amount: sum of amount for Q4 (Oct–Dec) of :year
  • total_transactions: total number of transactions for that coin in :year
  • total_amount: total sum of amount for that coin in :year

Notes / Requirements

  • Include coins with no transactions in :year (their amounts/counts should be 0).
  • Output columns exactly as listed above.

Per-coin quarterly transaction amounts with totals (conditional aggregation)

You are given two tables: coins(coin_id, name) and transactions(id, coin_id, dt, amount).

Write a SQL query that returns one row per coin with the following columns:

  • name
  • q1_amount: total transaction amount in Q1 (Jan–Mar)
  • q2_amount: total transaction amount in Q2 (Apr–Jun)
  • q3_amount: total transaction amount in Q3 (Jul–Sep)
  • q4_amount: total transaction amount in Q4 (Oct–Dec)
  • total_transactions: total number of transactions for the coin
  • total_amount: total transaction amount for the coin

If a coin has no transactions in a quarter, return 0 for that quarter. Use calendar quarters based on transactions.dt (assume all sample data is within the same year).

Table coins

columntypeconstraints
coin_idINTPRIMARY KEY, NOT NULL
nameVARCHAR(50)NOT NULL

Sample data:

coin_idname
1Bitcoin
2Ethereum
3Dogecoin

Table transactions

columntypeconstraints
idINTPRIMARY KEY, NOT NULL
coin_idINTNOT NULL
dtDATENOT NULL
amountDECIMAL(12,2)NOT NULL

Sample data:

amountcoin_iddtid
10012025-01-101
5012025-03-052
2512025-04-013
20012025-11-204
8022025-02-145
12022025-06-306
6022025-07-017
7.532025-05-108
1032025-08-159
532025-12-3110
7512025-09-0911

Expected output

One row per coin showing quarterly summed amounts (Q1–Q4), plus total transaction count and total amount. Quarters with no transactions should show 0.

nameq1_amountq2_amountq3_amountq4_amounttotal_amounttotal_transactions
Bitcoin15025752004505
Dogecoin07.510522.53
Ethereum801206002603

Hints

  1. Use conditional aggregation: SUM(CASE WHEN ... THEN amount END) for each quarter.
  2. LEFT JOIN from coins to transactions so coins with no transactions still appear (use COALESCE to return 0).