You are building a customer analytics system for an e-commerce platform. The system needs to evolve from a simple reporting tool to a flexible, reusable analytics engine. Complete the following parts in sequence — each part builds upon the previous one.
The marketing team needs a list of all customers who placed an order in the last 30 days, showing their Name, Email, and the Total Amount spent.
SQL Task:
Write a PostgreSQL query that:
customers and orders tablesorder_date within the last 30 days using CURRENT_DATE - INTERVAL '30 days'SUM() and GROUP BY to calculate total spendAPI Task
Create a REST API endpoint that:
GET /api/customers/recent-ordersThe admin dashboard needs a flexible search tool. Instead of writing new queries for every combination, you must make the Part A query dynamic.
SQL Task:
Create a PostgreSQL function search_orders() that:
customer_id INTEGER DEFAULT NULLmin_amount DECIMAL DEFAULT NULLstatus VARCHAR DEFAULT NULLNULLstatusamount > min_amount
CREATE OR REPLACE FUNCTION search_orders(
customer_id INTEGER DEFAULT NULL,
min_amount DECIMAL DEFAULT NULL,
status VARCHAR DEFAULT NULL
) RETURNS TABLE(...) AS $$
API Task
Create an endpoint that:
/api/orders/search (GET or POST)search_orders()The company wants a No-Code Report Builder. The frontend will send JSON describing filters for ANY table, and the backend must generate SQL dynamically.
SQL Task:
You must support this input:
{
"view_name": "SalesView",
"filters": [
{ "field": "Region", "op": "EQ", "val": "US-East" },
{ "field": "Revenue", "op": "GT", "val": 1000 }
]
}
Your solution must include:
EXECUTE & parameterization (USING)API Task
Create a dynamic endpoint that:
POST /api/reports/generate