The setup sounds manageable until you actually sit down to write it.
First, aggregate completed orders per restaurant over the last 30 days to compute total revenue. Then, use a window function or subquery to calculate the 25th percentile threshold across all restaurants, and finally filter orders to include only those from restaurants whose revenue is at or below that threshold.
Pro tip: Clarify the definition of 'completed orders' and 'revenue' (e.g., exclude refunds, include only delivered orders) and mention that percentile calculations may vary by SQL dialect (e.g., PERCENTILE_CONT vs. APPROX_QUANTILES). Also, consider whether to include restaurants with zero completed orders in the percentile calculation.
Identify the orders table and filter for completed orders within the last 30 days. Ensure you understand what 'completed' means (e.g., status = 'delivered') and how revenue is calculated (e.g., sum of order total).
Group by restaurant_id and sum the revenue to get total completed-order revenue for each restaurant over the last 30 days.
Calculate the 25th percentile of total revenue across all restaurants. This can be done using a window function like PERCENTILE_CONT(0.25) or a subquery with ORDER BY and LIMIT/OFFSET, depending on the SQL dialect.
Select restaurant_ids where total revenue is less than or equal to the 25th percentile value. Be mindful of whether to include restaurants with zero revenue.
Join the original orders table (filtered for last 30 days and completed status) with the list of bottom-quartile restaurants to return all relevant orders.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.