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
SQL Joins, CTEs, and Pandas Strings
00:00
5 left

SQL Joins, CTEs, and Pandas Strings

HardSQL · PostgreSQL

Problem

How would you answer SQL questions on joins and CTEs, and Python questions using pandas for string manipulation?

For the SQL portion, write a query that analyzes merchants, customers, payments, and payment events for Q1 2025. The result should combine payment totals with repeat-customer and month-over-month metrics.

Output

  1. One row per merchant, including merchants without qualifying payments.
  2. Columns: merchant_name, total_success_amount, active_customers, repeat_customers, latest_success_date, and avg_monthly_change.
  3. Include only successful payments from Q1 2025 for payment metrics. Order by total_success_amount descending, then merchant_name ascending.

Output details

  • Treat Q1 2025 as dates from 2025-01-01 inclusive through 2025-04-01 exclusive.
  • Define active_customers as distinct non-NULL customers with successful payments during Q1 2025.
  • Define repeat_customers as customers with successful payments in multiple distinct Q1 months for the same merchant.
  • Compute avg_monthly_change from consecutive monthly customer totals, average qualifying differences, use 0 when none, and round to 2 decimals.
  • Use 0 for merchants without qualifying payments, and leave latest_success_date as NULL when none exists.
  • Treat merchants as distinct records by merchant identity, retaining separate rows when merchant names repeat.

Schema

merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
merchant_nameVARCHAR(100)Merchant display name
regionVARCHAR(50)Merchant operating region
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer name
emailVARCHAR(150)Customer email address
payments
ColumnTypeDescription
payment_idPKINTUnique payment identifier
merchant_idINTMerchant receiving the payment
customer_idINTCustomer associated with the payment
amountNUMERIC(12,2)Payment amount
payment_statusVARCHAR(20)Payment lifecycle status
created_atDATEPayment creation date
payment_events
ColumnTypeDescription
event_idPKINTUnique payment event identifier
payment_idINTPayment associated with the event
event_typeVARCHAR(30)Payment event type
event_atTIMESTAMPEvent timestamp
Tablesmerchantscustomerspaymentspayment_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four 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.