cswork / libraryOpen practice workspace

Coding questions · PayPal

Analyze Transactions and Classify by Amount in SQL

This question evaluates SQL data manipulation competencies, including aggregation, grouping vs HAVING for filtered aggregations, and conditional classification using CASE WHEN on transactional data.

transactions

+---------------+---------+--------+---------+---------------------+-----------------+
| transaction_id| user_id | amount | status  | ts                  | payment_country |
+---------------+---------+--------+---------+---------------------+-----------------+
| 101           | 1       | 120.50 | success | 2023-04-01 10:00:00 | US              |
| 102           | 2       | 980.00 | fraud   | 2023-04-02 11:30:00 | CN              |
| 103           | 1       | 250.00 | success | 2023-04-03 09:45:00 | UK              |
| 104           | 3       | 1500.00| success | 2023-04-03 12:05:00 | US              |
+---------------+---------+--------+---------+---------------------+-----------------+
Scenario

You are asked to explore the transactions table and answer ad-hoc SQL questions during a live interview.

Question

Return each user_id and the total amount of their successful transactions. 2) Add a column that classifies each transaction as 'small' (< 100), 'medium' (100- 999), or 'large' (>= 1000) using CASE WHEN. 3) List users whose total successful transaction amount exceeds 1,000; use an aggregation in the HAVING clause.

Hints

Be explicit with CASE syntax, GROUP BY vs HAVING, and mention dialect differences if relevant.

Total success amount per user

Return each user_id and the total amount of their successful transactions.

Table transactions

columntypeconstraints
transaction_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
statusVARCHAR(20)NOT NULL
tsTIMESTAMPNOT NULL
payment_countryVARCHAR(2)NOT NULL

Sample data:

amountpayment_countrystatustransaction_idtsuser_id
120.5USsuccess1012023-04-01 10:00:001
980CNfraud1022023-04-02 11:30:002
250UKsuccess1032023-04-03 09:45:001
1500USsuccess1042023-04-03 12:05:003

Expected output

One row per user with the sum of amounts for transactions where status = 'success'.

total_success_amountuser_id
370.51
15003

Hints

  1. Filter rows with status = 'success'.
  2. Use SUM(amount) grouped by user_id.

Classify transactions by amount

Add a column that classifies each transaction as 'small' (<100), 'medium' (100-999), or 'large' (>=1000) using CASE.

Table transactions

columntypeconstraints
transaction_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
statusVARCHAR(20)NOT NULL
tsTIMESTAMPNOT NULL
payment_countryVARCHAR(2)NOT NULL

Sample data:

amountpayment_countrystatustransaction_idtsuser_id
120.5USsuccess1012023-04-01 10:00:001
980CNfraud1022023-04-02 11:30:002
250UKsuccess1032023-04-03 09:45:001
1500USsuccess1042023-04-03 12:05:003

Expected output

All transactions with an amount_class column derived from amount thresholds.

amountamount_classtransaction_iduser_id
120.5medium1011
980medium1022
250medium1031
1500large1043

Hints

  1. Use a CASE expression based on amount.
  2. Order of CASE conditions matters.

Users exceeding 1000 total

List users whose total successful transaction amount exceeds 1,000 using HAVING.

Table transactions

columntypeconstraints
transaction_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
statusVARCHAR(20)NOT NULL
tsTIMESTAMPNOT NULL
payment_countryVARCHAR(2)NOT NULL

Sample data:

amountpayment_countrystatustransaction_idtsuser_id
120.5USsuccess1012023-04-01 10:00:001
980CNfraud1022023-04-02 11:30:002
250UKsuccess1032023-04-03 09:45:001
1500USsuccess1042023-04-03 12:05:003

Expected output

Users whose sum of successful transaction amounts is greater than 1000.

total_success_amountuser_id
15003

Hints

  1. Aggregate with GROUP BY user_id.
  2. Filter aggregates using HAVING SUM(amount) > 1000.