Skip to main content
All Questions

Revenue by Customer City

Medium
Unlock detailed company stats for this questionUpgrade

Given the schema shown below, write a solution to fetch the total transaction revenue per user city, ordered by descending revenue (in USD).

users products +---------------+---------+ +-----------------+---------+ +--| id | int | +-----| id | int | | | first_name | varchar | | | name | varchar | | | last_name | varchar | | +->| product_line_id | date | | | user_city | int | | | | stock | int | | | email | int | | | +-----------------+---------+ | +---------------+---------+ | | | | | | transactions | | product_lines | +---------------+---------+ | | +--------+--------+ | | id | int |<----+ +--| id | int | +---->| customer_id | int | | name | varchar| | product_id | int | +--------+--------+ | amount | int | | currency_code | varchar | | date | date | +---------------+---------+ exchange_rate +----------------------+---------+ | id | int | | source_currency_code | varchar | | target_currency_code | varchar | | rate | numeric | +----------------------+---------+

Your answer should return a result with the following format:

user_city | total_revenue -----------+------------------------ varchar | float (no need to round)

Related courses

Course

Data Analyst Interview Prep

Ace your data analyst interviews—whether you're targeting product, marketing, or business analyst roles. Tackle real take-home case studies, sharpen your technical and dashboarding skills, and get strategies from interviewers at top tech companies and startups.

Course

SQL Interviews

Prepare for SQL interviews by practicing syntax basics, aggregations, and window functions. Learn how to manipulate data, write complex queries, and analyze information from diverse data sets.