Sql workflow

From SignalPilot-Labs/SignalPilot · View interactive version

Sql workflow is an AI agent skill from the GitHub repository SignalPilot-Labs/SignalPilot. Use this skill before writing any SQL query. Covers: output shape inference (cardinality clues from the question), efficient schema exploration, iterative CTE-based query building, structured verification loop (row count, NULL audit, fan-out check, sample inspection), error recovery protocol, saving

How to use this skill

Skill file

SQL Workflow Skill

1. Schema Exploration - Do This First

Before writing any SQL, understand the data:

  1. Read local schema files first (if schema/ directory exists in workdir):

Reading these files costs zero tool calls and gives you table structure + sample data.

Only call MCP tools for information not in the local files (e.g., row counts, live data exploration).

  1. Call list_tables to get all schemas and tables - only if no local schema files exist or you need row counts.
  2. Call describe_table on the tables that seem relevant to the question (only if JSON files lack detail)
  3. Call explore_column on categorical columns to see distinct values (for filtering/grouping)
  4. Call find_join_path if you need to join tables and the relationship is unclear

Stop exploring after 3-5 tool calls. Write SQL based on what you've found.

2. Output Shape Inference - Before Writing SQL

Read the task question carefully for cardinality clues:

Write a comment at the top of your SQL:

-- EXPECTED: <row count estimate> rows because <reason from question>

Critical checks:

3. Iterative Query Building - Build Bottom-Up

Do NOT write a 50-line query and run it all at once:

  1. Write the innermost subquery or first CTE first
  2. Run it standalone with query_database - verify row count and sample values
  3. Add the next CTE, verify again
  4. Continue until the full query is built

Example incremental pattern:

-- Step 1: verify source
SELECT COUNT(*) FROM orders WHERE status = 'completed';

-- Step 2: verify join partner cardinality
SELECT COUNT(*), COUNT(DISTINCT customer_id) FROM orders;

-- Step 3: build first CTE, verify
WITH order_totals AS (
  SELECT customer_id, SUM(amount) AS total
  FROM orders
  GROUP BY customer_id
)
SELECT COUNT(*), COUNT(DISTINCT customer_id) FROM order_totals;

-- Step 4: add final aggregation

4. Execution and Structured Verification

mcp__signalpilot__query_database
  connection_name="<task_connection_name>"
  sql="SELECT ..."

After executing, run these checks IN ORDER before saving:

  1. Row count sanity: Does 0 rows make sense? Does 1M rows make sense for a "top 10" question?
  2. Column count: Does the result have the right number of columns for the question?
  3. NULL audit: For each key column - unexpected NULLs indicate wrong JOINs:

```sql

SELECT COUNT(*) - COUNT(col) AS nulls FROM (your_query) t

```

  1. Sample inspection: Look at 5 rows - are values in expected ranges? Do string columns have meaningful values (not join keys)?
  2. Fan-out check: If JOINing, compare COUNT(*) vs COUNT(DISTINCT primary_key):

```sql

SELECT COUNT(*) AS total_rows, COUNT(DISTINCT <pk>) AS unique_keys FROM (your_query) t;

```

If they differ, you have duplicate rows from a fan-out JOIN.

  1. Re-read the question: Does your output actually answer what was asked?

5. Error Recovery Protocol

```sql

SELECT COUNT(*) FROM table WHERE cond_1; -- still same? keep it

SELECT COUNT(*) FROM table WHERE cond_1 AND cond_2; -- drops? cond_2 is culprit

```

6. Saving Output

Once you have the correct result:

  1. Write final SQL to result.sql:

```

Write tool: path="result.sql", content="<your SQL query>"

```

  1. Write the result as CSV to result.csv:

```

Write tool: path="result.csv", content="col1,col2,...\nval1,val2,..."

```

7. Turn Budget Management

If your query works and passes all verification checks, SAVE IMMEDIATELY - do not continue exploring "just in case".

8. Common Query Traps

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.