Column names in this schema are non-standard and differ from common conventions (e.g. search_term not query, cost not spend). Call list_tables first to confirm exact table and column names — a query built on assumed names will fail.
Execute a paginated, read-only SELECT query against any allowlisted analytics table.
Unlike query_brand_analytics (which uses a virtual "data" table), here you write SQL
with the real table names. Every table referenced must be in the server-side allowlist.
Rules:
- SQL must be a SELECT statement.
- All tables referenced must be in the allowlist — access to system or user tables is denied.
- Include WHERE profile_id = '<profileId>' in the query for data scoping.
- Parameter placeholders ($1, $2) are not supported in the SQL — use literal values.
- Results are paginated: max 1000 rows per call. Increment page to iterate.
- Additional WHERE filters (e.g. adType, state, campaignType, matchType) should reflect only what the user explicitly asked for.
- A LIMIT or OFFSET clause in the SQL is rejected — use the `limit`/`page` parameters for pagination, and ORDER BY + WHERE for a "top N" request or a specific count.
- After each call inspect row_count, page, and limit: if row_count === limit, more rows exist — fetch the next page. Stop after 10 pages / 10 000 total rows and inform the user.
- When a query term is ambiguous (e.g. "main campaigns", "top keywords"), state your interpretation before showing results, or ask the user to clarify.
- GROUP BY should use an ID column rather than a name column when one is available (e.g. campaign_id, not campaign_name) — names can change, IDs are stable. A name column is appropriate for GROUP BY only when no ID exists for that entity.
- Filtering by a campaign or ad-group name requires first resolving it to an ID via a lookup SELECT, then using the ID in subsequent WHERE clauses — names are mutable, IDs are not.
- The SQL must be a single statement — a trailing semicolon is ignored; a second statement after a semicolon is rejected. CTEs (WITH ... SELECT ...) count as one statement and are supported.
- When the SQL joins tables, one WHERE profile_id = '<profileId>' is enough — every joined table is scoped to that profile automatically.
- AVG() on pre-computed rate columns (acos, ctr, cvr, cpc) runs without error but returns a mathematically wrong result (average-of-averages) — compute rates from their source numerators/denominators instead.
Allowlisted tables: sales_and_traffic_by_asin_daily, sales_and_traffic_by_date_daily, purchased_products, campaigns, campaign_metrics, campaign_inventory_state, ad_groups, keywords, targets, portfolios, brand_terms, search_term_tags, search_term_brand_classifications, placement_reports, targeting_reports, search_term_reports, advertised_products, gross_and_invalid_traffic_reports, vendor_product_catalog, vendor_daily_metrics, vendor_procurement_metrics, vendor_inventory_snapshots, vendor_manufacturing_forecasts, vendor_customer_feedback, vendor_geo_sales, vendor_purchase_orders, vendor_purchase_orders_status, vendor_invoice_headers, vendor_invoice_items, vendor_remittance_headers, vendor_remittance_items, vendor_cogs_history, vendor_terms_of_trade_history
ERROR QUALITY NOTE: unknown table errors return a specific message naming the table — call list_tables to discover available names. Profile_id scoping errors also return a specific message. All other validation failures (blocked keywords, syntax errors) return a generic "SQL query validation failed" message — if you get this, re-check your SQL structure.
SETTLEMENT LAG — Amazon data is not instantly final. When your query covers recent dates, caveat figures to the user accordingly:
- Ad-metric tables (campaign_metrics, advertised_products, etc.): data for the last 3 days is provisional — spend, impressions, and attributed sales may increase over the next 72 hours.
- Order/SP-API tables (amazon_orders, etc.): data for the last 14 days may be revised as returns and adjustments are processed by Amazon.
- SP-API Sales & Traffic tables (sales_and_traffic_by_asin_daily, sales_and_traffic_by_date_daily): session and traffic counts for periods closed within the last 3 days may be incomplete — Amazon processes period data within 72 hours of a period closing.
DATA COVERAGE — separate from settlement lag above, a profile may have NO ingested data at all before
some start date (e.g. a profile onboarded in 2026 has nothing before then). A query with a WHERE date
filter outside that range returns no rows (or NULL aggregates) — this looks identical to "no sales",
but means something different ("we never ingested this period"). Before answering any question involving
a date range, get that table's min_date/max_date by calling list_tables with includeDateContext:true —
a default list_tables call returns schema only and does NOT include those bounds. If the requested range
falls partially or fully outside [min_date, max_date], say so explicitly before presenting results — do not
silently present the empty result as if it were a real zero-sales period.