Write Transaction Analytics SQL Queries
ZooxData Engineer · Technical ScreenLast updated
This question evaluates proficiency in SQL for analytical queries, covering aggregation, time-based filtering, joins between transactions and vendor metadata, and the use of ranking/window functions for top-N calculations.
You are given a credit-card transaction dataset with the following tables.
transactions
| column | type | description |
|---|---|---|
transaction_id | integer | Unique ID for a transaction. |
user_id | integer | Unique ID for a customer. |
vendor_id | integer | Unique ID for a vendor. |
transaction_time | timestamp | Time when the transaction was recorded. |
transaction_dollars | numeric | Dollar amount of the transaction. |
transaction_type | text | Transaction type, such as PURCHASE or REFUND. |
refund_transaction_id | integer | For a REFUND, the transaction_id of the original PURCHASE. |
vendors
| column | type | description |
|---|---|---|
vendor_id | integer | Unique ID for a vendor. |
city | text | City where the vendor is located. |
state_province | text | State or province where the vendor is located. |
country | text | Two-letter country code. |
Assume that only PURCHASE rows count as charged dollars.
Write SQL queries for the following tasks:
- Find the top 3 vendors by total purchase dollars charged in the last 2 years. Return
vendor_idandtotal_dollars_charged, sorted from highest to lowest total. - For each
(state_province, country)location, find the top 3 vendors by total purchase dollars charged in the last 2 years. Returnstate_province,country,vendor_id,total_dollars, andvendor_rank. Use dense ranking so ties receive the same rank.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.