# `query_tabular_data` — Query CSV & JSON Tables with SQLite

> Part of [Sheet SQL & Multi-Tab XLSX Engine API](https://sheet-sql.apikeg.com/llms.txt) (`com.apikeg/sheet-sql`)

Ingests 1–10 named RFC 4180 CSV/TSV or JSON row-array datasets into an ephemeral zero-retention SQLite sandbox, coerces currency columns (e.g. '$1,250.50' -> 1250.5), runs read-only SQL (JOIN, GROUP BY, CTEs, window functions), and returns typed rows, Markdown preview, inferred schemas, and optional .xlsx export.

**Why call instead of DIY:** Replaces fragile regex CSV parsers, LLM mental math errors, and heavy container sandboxes; runs real SQLite in <25ms with guaranteed ephemeral wipe.

- **REST Endpoint:** `POST https://sheet-sql.apikeg.com/v1/query-tabular-data`
- **MCP Server:** `https://sheet-sql.apikeg.com/mcp` (tool: `query_tabular_data`)
- **Pricing:** $0.002 base (up to $0.01 max $0.002 base + $0.001 per 1,000 total input rows (capped at $0.01), x402 variable settlement)
- **Free Dry-Run:** `POST https://sheet-sql.apikeg.com/v1/query-tabular-data?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "tables": {
      "type": "object",
      "additionalProperties": {
        "type": "string",
        "minLength": 1,
        "maxLength": 3800000,
        "description": "Raw RFC 4180 CSV/TSV string (auto-detecting ',', ';', or '\\t') or serialized JSON row-array string."
      },
      "description": "Map of 1 to 10 SQL table names (e.g. 'orders', 'customers') to CSV/TSV or JSON row-array payloads."
    },
    "sql": {
      "type": "string",
      "minLength": 1,
      "maxLength": 32768,
      "description": "Read-only SQLite SELECT or WITH (CTE) query supporting JOIN, GROUP BY, HAVING, and window functions (ROW_NUMBER() OVER (...), SUM, AVG)."
    },
    "coerceCurrencyNumbers": {
      "type": "boolean",
      "default": true,
      "description": "Automatically strip $, €, £, ¥ symbols, thousands commas, and accounting parentheses (e.g. '$1,250.50' -> 1250.5) when an entire column is numeric."
    },
    "maxRows": {
      "type": "integer",
      "minimum": 1,
      "maximum": 5000,
      "default": 1000,
      "description": "Maximum result rows returned in `rows` (1–5000, default 1000)."
    },
    "markdownPreviewRows": {
      "type": "integer",
      "minimum": 1,
      "maximum": 100,
      "default": 20,
      "description": "Maximum rows rendered in `markdownPreview` (1–100, default 20)."
    },
    "exportXlsx": {
      "type": "boolean",
      "default": false,
      "description": "When true, also exports the query result set as a styled OpenXML .xlsx workbook in `xlsxBase64`."
    },
    "xlsxSheetName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 64,
      "description": "Optional worksheet tab name when `exportXlsx` is true (default 'Query Results')."
    },
    "xlsxSummaryRowFormulas": {
      "type": "object",
      "additionalProperties": {
        "type": "string",
        "enum": [
          "SUM",
          "AVERAGE",
          "MIN",
          "MAX",
          "COUNT",
          "COUNTA"
        ],
        "description": "Spreadsheet summary aggregation function (SUM, AVERAGE, MIN, MAX, COUNT, COUNTA)."
      },
      "description": "Optional summary row formulas keyed by output column name when `exportXlsx` is true (e.g. { total_revenue: 'SUM' })."
    }
  },
  "required": [
    "tables",
    "sql"
  ],
  "additionalProperties": false
}
```

## Examples
### JOIN orders CSV with customers JSON and rank revenue per region
```bash
curl -X POST "https://sheet-sql.apikeg.com/v1/query-tabular-data" \
  -H "Content-Type: application/json" \
  -d '{"coerceCurrencyNumbers":true,"maxRows":100,"markdownPreviewRows":10,"exportXlsx":true,"tables":{"orders":"order_id,customer_id,amount\n101,c1,\"$1,250.50\"\n102,c1,\"$749.50\"\n103,c2,\"$500.00\"","customers":"[{\"id\":\"c1\",\"name\":\"Acme Corp\",\"region\":\"US\"},{\"id\":\"c2\",\"name\":\"Globex\",\"region\":\"EU\"}]"},"sql":"SELECT c.name, c.region, SUM(o.amount) AS total_spend, ROW_NUMBER() OVER (ORDER BY SUM(o.amount) DESC) AS spend_rank FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.id, c.name, c.region ORDER BY total_spend DESC"}'
```
Expected output: Returns typed rows (Acme Corp total_spend=2000, spend_rank=1), GFM markdownPreview, inferredSchemas showing amount coerced to REAL, and base64 .xlsx.
