HARDVerified Real Interview• Analytics Engineer

Customer Peak Spending Day (Chained CTE)

Amazon Prime analytics wants to identify each customer's single highest spending calendar day.

Write a SQL query using Common Table Expressions (WITH) to find each customer's peak spend day. Return customer_id, order_date, and daily_spend. If a customer tied their peak spend across multiple days, output the most recent date. Sort by daily_spend descending, then customer_id ascending.

Tables:

  • orders (order_id INT, customer_id INT, order_date DATE, amount DECIMAL)

Execution Sandbox:

Your query executes inside an isolated in-memory PostgreSQL engine (PGlite WebAssembly). You have full access to standard PostgreSQL aggregations, window functions, and CTEs.

Standard SQL (ANSI)|Ln 1, Col 1
⌘↵ Run Query
Ready to execute. Press Run (⌘+Enter)
Write your query and click “Run” or press ⌘+Enter to inspect output.