212-sql-correctness

Eine Prüfliste für korrekte SQL-Abfragen, also Befehle, mit denen Datenbanken gelesen und verändert werden.

In plain words
What is it for?
Zum Prüfen von SQL-Abfragen für PostgreSQL, MySQL, BigQuery, Snowflake und andere Datenbanken, besonders bei Joins, Datumsfunktionen, JSON-Daten und NULL-Werten.
Why use it?
Sie hilft, Fehler durch unterschiedliche Datenbankvarianten, falsche Verknüpfungen, unklare Spaltennamen und fehlerhafte Filter zu vermeiden.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/hamzaamjad/cursor-rules/212-sql-correctness
Clone the repo
git clone --depth 1 https://github.com/hamzaamjad/cursor-rules
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 901 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00901
Opus 5 $0.00000 $0.00451
Sonnet 5 $0.00000 $0.00180
Haiku 4.5 $0.00000 $0.00090

Measured yesterday against content hash 579a46ace1bb, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

212-sql-correctness scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/200-domain/212-sql-correctness.mdc · 44 lines

What it actually says

sql-correctness.mdc

  • Purpose: To guide AI assistants and developers in writing correct and robust SQL queries, paying attention to dialect specifics and common pitfalls.
  • Requirements:
    1. Dialect Awareness:
      • Explicitly confirm the target SQL dialect (e.g., Redshift, PostgreSQL, MySQL, BigQuery, Snowflake).
      • Verify function usage against the specific dialect's documentation, especially for date/time manipulation (e.g., DATEADD/DATE_ADD/+ INTERVAL, DATEDIFF, GETDATE/NOW/CURRENT_TIMESTAMP), string manipulation, and JSON functions.
    2. Join Logic:
      • Verify join types (INNER, LEFT, RIGHT, FULL OUTER) match the intended logic for handling matching and non-matching rows.
      • Ensure join conditions (ON clause) correctly link tables using appropriate keys and comparisons. Avoid unintentional cross joins.
    3. Column References:
      • Qualify column names with table aliases or full table names when multiple tables are involved to avoid ambiguity.
      • Double-check column names for typos against the source schema.
    4. Filtering:
      • Ensure WHERE clauses accurately reflect the desired filtering conditions.
      • Be mindful of NULL handling in comparisons (use IS NULL / IS NOT NULL).
    5. Aggregation & Window Functions:
      • Verify GROUP BY clauses include all non-aggregated columns in the SELECT list (unless the dialect permits otherwise).
      • Ensure window function PARTITION BY and ORDER BY clauses are correctly specified for the intended calculation.
      • Redshift Specific: Avoid using COUNT(DISTINCT col) OVER (PARTITION BY ...) due to potential parser limitations. Instead, pre-aggregate data to the required grain in a preceding CTE to ensure distinctness, then use COUNT(*) OVER (PARTITION BY ...) on the pre-aggregated data.
    6. Output Ordering:
      • Include an ORDER BY clause for final SELECT statements intended for human consumption or reporting to ensure deterministic results, unless ordering is irrelevant or handled downstream.
    7. Syntax & Formatting:
      • Validate overall query syntax against the target dialect.
      • Maintain consistent formatting (indentation, capitalization of keywords) for readability. Refer to sql-performance.mdc for performance-related style.
  • Validation:
    • Check: Is the target SQL dialect mentioned or assumed correctly?
    • Check: Are dialect-specific functions used appropriately? (e.g., Redshift DATEADD vs. PostgreSQL interval math).
    • Check: Is join logic clear and likely correct? Are columns qualified?
    • Check: Does the query include an ORDER BY clause if the output is likely for reporting?
    • Check: Is the syntax valid for the target platform?
  • Examples:
    • Scenario: Calculating days between two dates in Redshift.
      • Weak (PostgreSQL style): SELECT end_date - start_date FROM my_table;
      • Improved (Redshift style): SELECT DATEDIFF(day, start_date, end_date) FROM my_table;
    • Scenario: Getting user name but handling missing users.
      • Weak (Might error or lose rows on INNER JOIN): SELECT o.order_id, u.name FROM orders o JOIN users u ON o.user_id = u.id;
      • Improved (Handles missing users): SELECT o.order_id, COALESCE(u.name, 'Unknown') FROM orders o LEFT JOIN users u ON o.user_id = u.id;
  • Changes: Updated to include recent SQL dialects and best practices for ensuring SQL correctness, including handling of JSON data types and new window functions.
  • Source References: Retrospective from GTM compensation SQL task (July 2024).
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 44 lines · 0 tokens per session scan A 579a46ace1bb

Subscribe to this mod's changes

212-sql-correctness is a cursor rule published in the GitHub repository hamzaamjad/cursor-rules (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 901 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.