Prompt Chain

Validate SQL Queries Against Expected Outputs

A multi-step prompt workflow that validates SQL queries for correctness, security vulnerabilities, and best practices before execution in production

Works with github

54
Spark score
out of 100
Updated yesterday
Version code-scan-action-0.1

Add to Favorites

Why it matters

Ensure the accuracy and correctness of your SQL queries by automatically validating their output against predefined expectations. This asset helps catch errors and regressions before they impact your data.

Outcomes

What it gets done

01

Execute SQL queries against a database.

02

Compare query results to expected outcomes.

03

Identify discrepancies and potential errors in SQL.

04

Automate the testing of SQL query logic.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/pfoo-sql-validation | bash

Capabilities

What this chain does

Query a database

Writes and executes SQL or NoSQL queries on databases.

Write tests

Creates unit, integration, or end-to-end test cases.

Debug

Traces errors to their root cause and suggests fixes.

Extract

Pulls structured data fields from unstructured text.

Overview

Sql Validation

What it does

This prompt chain implements a structured, multi-step workflow for validating SQL queries. It systematically evaluates queries for syntax correctness, security vulnerabilities like SQL injection, and adherence to performance best practices. The workflow is designed to run as an automated validation pipeline using the promptfoo framework.

How it connects

Use this when you need to validate SQL queries before production deployment, especially in environments where untrusted input may influence query construction. It's ideal for teams building database-backed applications who want to catch SQL errors, security risks, and performance issues during development rather than in production.

Source README

yaml-language-server: $schema=https://promptfoo.dev/config-schema.json

description: 'Is-SQL example'

prompts:
Output a SQL query that returns the number of {{product}} sold in the last month.

Database schema:
{{database}}

Only output raw SQL code without markdown.

providers:

  • openai:gpt-4.1-mini
  • openai:gpt-4o

tests:

  • vars:
    product: bananas
    database: file://database.sql

    assert:

    Ensure output is a valid SQL query

    • type: is-sql
    • type: icontains
      value: 'Select'
  • vars:
    product: apples
    database: file://database.sql
    assert:

    Ensure output is a valid SQL query

    • type: is-sql
      value:
      databaseType: 'MySQL'

      Ensure only the following tables are used

      allowedTables:
      - select::null::Products
      - select::null::Shipments
  • vars:
    product: oranges
    database: file://database.sql
    assert:

    • type: is-sql
      value:
      databaseType: 'MySQL'
      allowedColumns:
      # We expect this test to fail no matter what
      - select::null::DoesntExist

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.