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
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
Execute SQL queries against a database.
Compare query results to expected outcomes.
Identify discrepancies and potential errors in SQL.
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
Writes and executes SQL or NoSQL queries on databases.
Creates unit, integration, or end-to-end test cases.
Traces errors to their root cause and suggests fixes.
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.sqlassert:
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
- type: is-sql
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
- type: is-sql
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.