Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Writing Complex Queries

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Writing Complex Queries. Start with the requirements and the four tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

How would you write complex queries to answer a business question?

Using the supplied customer, order, order item, and support ticket data, produce a monthly performance view for active customers from January through March 2025. Include completed-order revenue, month-over-month revenue change, regional revenue rank, a three-month rolling revenue total, and support ticket volume.

Output

  1. One row per active customer and calendar month.
  2. Columns: customer_id, customer_name, region, month, monthly_revenue, prior_month_revenue, revenue_growth_pct, regional_monthly_rank, rolling_3_month_revenue, and support_ticket_count.
  3. Include months with no revenue as zero, order by month, region, regional_monthly_rank, and customer_id.

Schema

customers
ColumnTypeDescription
customer_idPKINTCustomer identifier
customer_nameVARCHAR(100)Customer display name
regionVARCHAR(30)Customer sales region
is_activeBOOLEANWhether the customer is active
orders
ColumnTypeDescription
order_idPKINTOrder identifier
customer_idINTOrdering customer
order_dateDATEDate the order was placed
order_statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTOrder item identifier
order_idINTParent order
quantityINTQuantity purchased
unit_priceNUMERIC(10,2)Price per unit
discountNUMERIC(10,2)Order-line discount
support_tickets
ColumnTypeDescription
ticket_idPKINTSupport ticket identifier
customer_idINTCustomer who opened the ticket
opened_atDATETicket opening date
ticket_statusVARCHAR(20)Ticket status
Tablescustomersordersorder_itemssupport_tickets
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results