Sql Best Practices logo

Sql Best Practices

Community
Omar-Obando
sql-best-practices

SQL best practices skill covering query writing, indentation, N+1 prevention, indexing, performance, and security. Every SQL query must follow these standards regardless of the database engine.

Overview

PublisherOmar-Obando
Repositoryqwen-orchestrator
Skill namesql-best-practices
Stars
53
Forks
13
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by Omar-Obando on GitHub. Read the source before you install it.

Installation

Install the Sql Best Practices AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/Omar-Obando/qwen-orchestrator/tree/main/skills/sql-best-practices
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Omar-Obando/qwen-orchestrator.git /tmp/qwen-orchestrator
mkdir -p .claude/skills
cp -r /tmp/qwen-orchestrator/skills/sql-best-practices .claude/skills/sql-best-practices
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Sql Best Practices in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Sql Best Practices on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Sql Best Practices is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

SQL Best Practices — Every Query a Masterpiece

This skill enforces SQL quality standards: proper indentation, N+1 prevention, parameterized queries, index-friendly patterns, and security.

SQL Indentation Standard

EVERY SQL query MUST be properly indented:

sql
-- ✅ CORRECT: Properly formatted
SELECT
    u.id,
    u.name,
    u.email,
    u.created_at,
    r.name  AS role_name,
    d.name  AS department_name
FROM
    users u
    INNER JOIN roles r
        ON r.id = u.role_id
    LEFT JOIN departments d
        ON d.id = u.department_id
WHERE
    u.deleted_at IS NULL
    AND u.status = 'active'
    AND u.created_at >= '2024-01-01'
ORDER BY
    u.created_at DESC
LIMIT 20
OFFSET 0;
sql
-- ❌ WRONG: No formatting
select id,name,email from users where deleted_at is null and status='active' order by created_at desc limit 20;

Formatting Rules

  1. Keywords: UPPERCASE (SELECT, FROM, WHERE, JOIN, etc.)
  2. Indentation: Each clause on its own line, indented
  3. Columns: One per line in SELECT
  4. JOIN conditions: Indented under the JOIN
  5. WHERE conditions: Aligned AND/OR
  6. Commas: Leading or trailing — pick one, be consistent

N+1 Prevention Patterns

Detection Pattern

If you see a LOOP containing a QUERY, it's likely N+1.
Count queries: 1 (initial) + N (loop) = N+1 problem.

Solution: Eager Loading

sql
-- ❌ N+1: One query per order for items
SELECT * FROM orders WHERE user_id = 123;
-- Then for EACH order:
SELECT * FROM order_items WHERE order_id = ?;

-- ✅ Single query with JOIN
SELECT
    o.id           AS order_id,
    o.total        AS order_total,
    o.status       AS order_status,
    oi.id          AS item_id,
    oi.product_name,
    oi.quantity,
    oi.unit_price
FROM
    orders o
    INNER JOIN order_items oi
        ON oi.order_id = o.id
WHERE
    o.user_id = 123
    AND o.deleted_at IS NULL
ORDER BY
    o.created_at DESC,
    oi.id ASC;

Solution: Batch with IN

sql
-- ❌ N+1: Loop over IDs
SELECT * FROM products WHERE id = 1;
SELECT * FROM products WHERE id = 2;
SELECT * FROM products WHERE id = 3;

-- ✅ Batch: Single query
SELECT
    id,
    name,
    sku,
    price,
    stock_quantity
FROM
    products
WHERE
    id IN (1, 2, 3)
    AND deleted_at IS NULL;

Solution: Subquery for Aggregates

sql
-- ❌ N+1: Count per category in a loop
SELECT * FROM categories;
-- Then for EACH category:
SELECT COUNT(*) FROM products WHERE category_id = ?;

-- ✅ Single query with subquery
SELECT
    c.id,
    c.name,
    COALESCE(p.product_count, 0) AS product_count
FROM
    categories c
    LEFT JOIN (
        SELECT
            category_id,
            COUNT(*) AS product_count
        FROM
            products
        WHERE
            deleted_at IS NULL
        GROUP BY
            category_id
    ) p ON p.category_id = c.id
WHERE
    c.deleted_at IS NULL
ORDER BY
    c.name ASC;

Security Rules

Parameterized Queries (ALWAYS)

❌ NEVER: String concatenation or interpolation
✅ ALWAYS: Parameterized queries with placeholders (? or named)

Language-specific:
- TypeScript: pool.query('SELECT * FROM users WHERE id = ?', [userId])
- PHP/Laravel: User::where('id', $userId)->first()
- Python: cursor.execute('SELECT * FROM users WHERE id = %s', [user_id])
- Dart: query('SELECT * FROM users WHERE id = ?', [userId])

Access Control

sql
-- Always filter by tenant/user in multi-tenant systems
SELECT
    id, name, email
FROM
    users
WHERE
    tenant_id = :current_tenant_id    -- NEVER skip tenant filter
    AND deleted_at IS NULL;

Performance Rules

Index-Friendly Patterns

sql
-- ✅ Index-friendly: sargable
WHERE created_at >= '2024-01-01'
WHERE status = 'active'
WHERE email = 'user@example.com'

-- ❌ Index-hostile: non-sargable
WHERE YEAR(created_at) = 2024         -- Function on column
WHERE LOWER(email) = 'user@...'       -- Function on column
WHERE name LIKE '%search%'            -- Leading wildcard
WHERE id + 1 = 100                    -- Expression on column

SELECT Explicit Columns (NEVER SELECT *)

sql
-- ❌ NEVER
SELECT * FROM users

-- ✅ ALWAYS
SELECT
    id,
    name,
    email,
    status,
    created_at
FROM
    users

Comment Standards

sql
-- Purpose: Fetch active users with their role and department
-- Author: [Agent/User]
-- Performance: Uses idx_users_status_created, expected < 10ms
-- Security: Filters by tenant_id for data isolation
SELECT
    u.id,
    u.name,
    u.email,
    r.name  AS role_name,
    d.name  AS department_name
FROM
    users u
    INNER JOIN roles r ON r.id = u.role_id
    LEFT JOIN departments d ON d.id = u.department_id
WHERE
    u.tenant_id = :tenant_id           -- Data isolation
    AND u.deleted_at IS NULL            -- Soft delete filter
    AND u.status = 'active'             -- Active users only
ORDER BY
    u.name ASC;

When NOT to Use

Do NOT use this skill when:

  • Writing application business logic (use domain-driven skill)
  • Designing database schemas (use database-design skill)
  • Writing API endpoint implementations (use api-design skill)
  • Reviewing code for quality issues (use code-review skill)
  • Performing security audits (use security-auditor skill)
  • Analyzing deployment configurations (use deployment skill)
  • Designing multi-page website layouts (use design-system skill)
  • Reviewing git workflows (use git-workflow skill)

Frequently asked questions

What does the Sql Best Practices AI skill do?

SQL best practices skill covering query writing, indentation, N+1 prevention, indexing, performance, and security. Every SQL query must follow these standards regardless of the database engine.

Why use Sql Best Practices on TypingMind?

Because you install it once and use it with any model. Sql Best Practices is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Sql Best Practices in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/Omar-Obando/qwen-orchestrator/tree/main/skills/sql-best-practices. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Sql Best Practices?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Sql Best Practices?

As many as you like. As long as a model supports skills, you can use Sql Best Practices with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Sql Best Practices AI skill free?

Yes. It is published on GitHub by Omar-Obando under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇