Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Build an Active Customer Revenue Report
00:00
5 left

Build an Active Customer Revenue Report

HardSQL · PostgreSQL

Problem

Explain how you would use SQL to extract actionable insights from a messy, multi-source dataset.

Use the provided customer, sales order, and support ticket tables. Consider inconsistent status values, missing amounts, unmatched source records, inactive customers, and customers with no activity.

Output

  1. One row per active customer.
  2. Include customer_id, customer_name, region, total_revenue, completed_order_count, active_ticket_count, high_priority_active_tickets, last_order_date, and action_status.
  3. Include only orders from 2025-01-01 through 2025-03-31, include customers without matching orders or tickets, and sort by total_revenue descending, then customer_id ascending.

Output details

  • Treat a customer as active only when active is TRUE.
  • Normalize statuses, priorities, and order statuses by trimming whitespace and comparing case-insensitively.
  • Count only normalized completed orders; treat missing amounts as 0 for revenue.
  • Treat normalized ticket statuses open and pending as active, and count high priority only when normalized priority is high.
  • Set action_status to Escalate support for high-priority active tickets, Re-engage and resolve for zero revenue with active tickets, Prioritize account for revenue at least 500, otherwise Monitor.
  • Retain every active customer-table row, including duplicate customer identifiers and NULL customer attributes.

Schema

crm_customers
ColumnTypeDescription
customer_idPKINTCanonical customer identifier
customer_nameVARCHAR(100)Customer display name
regionVARCHAR(50)Customer sales region
emailVARCHAR(255)Customer email address
activeBOOLEANWhether the customer is currently active
sales_orders
ColumnTypeDescription
order_idPKINTSales order identifier
customer_idINTCustomer identifier from the sales source
order_dateDATEDate the order was recorded
amountNUMERIC(12,2)Order amount
order_statusVARCHAR(30)Raw order status from the sales source
support_tickets
ColumnTypeDescription
ticket_idPKINTSupport ticket identifier
customer_idINTCustomer identifier from the support source
opened_atDATEDate the ticket was opened
priorityVARCHAR(20)Raw support priority
statusVARCHAR(20)Raw support ticket status
Tablescrm_customerssales_orderssupport_tickets
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.