agentleFS
justanestajustanesta/claude-code-resources/skills/SQL/sql-query-fundamentals/SKILL.md

sql-query-fundamentals

Skill2 starsChanged 10 months agoMITView on GitHub

Core SQL query patterns for SELECT, JOINs, WHERE, GROUP BY, and subqueries. Use this skill when writing basic to intermediate SQL queries, need to understand join types, or work with filtering and aggregation. Covers SELECT fundamentals, all join types, subqueries, CTEs, CASE expressions, and common table operations.

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    SELECT only what you need: - Avoid SELECT * in production

    line 16
  2. Distinctive:

    Use explicit JOINs: - Never use implicit joins with WHERE

    line 17
  3. Distinctive:

    When to use subquery vs JOIN

    line 118
  4. Very specific:

    Use sargable predicates: WHERE date >= '2024-01-01' not WHERE YEAR(date) = 2024

    line 183
  5. Very specific:

    Avoid type casting in filters: WHERE id = 123 not WHERE id::text = '123'

    line 184
  6. Very specific:

    Use EXISTS over IN for correlated checks on large sets

    line 186

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SQL Query Fundamentals

Essential SQL patterns for querying relational databases.

Core Principles

  1. SELECT only what you need - Avoid SELECT * in production
  2. Use explicit JOINs - Never use implicit joins with WHERE
  3. Filter early - Apply WHERE before GROUP BY when possible
  4. Use CTEs for clarity - Make complex queries readable

Basic SELECT Patterns

SELECT
    customer_id,
    first_name,
    last_name,
    email
FROM customers
WHERE status = 'active'
ORDER BY last_name, first_name;

See select-patterns.md for:

  • Column aliases and calculated columns
  • DISTINCT vs GROUP BY
  • LIMIT/OFFSET pagination across databases
  • Column selection best practices

JOIN Types

-- INNER JOIN (only matching rows)
SELECT o.order_id, c.customer_name, o.order_date
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id;

-- LEFT JOIN + find unmatched rows
SELECT c.customer_id, c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;

See join-patterns.md for:

  • Join types decision matrix
  • Multiple table joins and self joins
  • Join conditions vs WHERE filters
  • Range joins and CROSS JOINs

WHERE Clause Patterns

WHERE age >= 18 AND age < 65
-- IN clause (better than multiple ORs)
WHERE country IN ('USA', 'Canada', 'Mexico')
-- BETWEEN for ranges
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'
-- NULL handling
WHERE COALESCE(mobile_phone, home_phone) IS NOT NULL

See where-clause-patterns.md for:

  • Operator precedence and pattern matching
  • NULL handling strategies
  • Performance considerations (sargable queries)

GROUP BY and Aggregations

SELECT
    category,
    COUNT(*) as product_count,
    AVG(price) as avg_price,
    SUM(price * quantity) as total_revenue
FROM products
GROUP BY category
HAVING COUNT(*) > 5 AND SUM(price * quantity) > 1000
ORDER BY total_revenue DESC;

See groupby-aggregation.md for:

  • Common aggregate functions
  • GROUP BY vs DISTINCT
  • HAVING vs WHERE

Subqueries

-- Correlated subquery: employees above department average
SELECT
    e.employee_name,
    e.salary,
    (SELECT AVG(salary)
     FROM employees e2
     WHERE e2.department_id = e.department_id) as dept_avg
FROM employees e
WHERE e.salary > (
    SELECT AVG(salary) FROM employees e3
    WHERE e3.department_id = e.department_id
);

See subquery-patterns.md for:

  • Subquery types (scalar, column, row, table)
  • Correlated vs non-correlated
  • EXISTS vs IN performance
  • When to use subquery vs JOIN

Common Table Expressions (CTEs)

WITH
    high_value_orders AS (
        SELECT * FROM orders WHERE total_amount > 1000
    ),
    vip_customers AS (
        SELECT customer_id, COUNT(*) as high_value_count
        FROM high_value_orders
        GROUP BY customer_id
        HAVING COUNT(*) >= 3
    )
SELECT c.customer_name, c.email, vc.high_value_count
FROM customers c
INNER JOIN vip_customers vc ON c.customer_id = vc.customer_id;

See cte-patterns.md for:

  • CTE vs subquery trade-offs
  • Recursive CTEs for hierarchical data
  • Materialized CTEs (PostgreSQL)

CASE Expressions

SELECT
    category,
    COUNT(*) as total_products,
    COUNT(CASE WHEN price < 20 THEN 1 END) as budget_count,
    COUNT(CASE WHEN price BETWEEN 20 AND 100 THEN 1 END) as standard_count,
    COUNT(CASE WHEN price > 100 THEN 1 END) as premium_count,
    AVG(CASE WHEN in_stock = true THEN price END) as avg_available_price
FROM products
GROUP BY category;

See case-expressions.md for:

  • Simple vs searched CASE
  • Conditional aggregation patterns
  • Pivot table simulation with CASE

Cross-Database Compatibility

FeaturePostgreSQLMySQLSQL Server
String concat||CONCAT()+
Date add+ INTERVAL '7 days'DATE_ADD(d, INTERVAL 7 DAY)DATEADD(day, 7, d)
Limit rowsLIMIT N OFFSET MLIMIT N OFFSET MOFFSET M ROWS FETCH NEXT N ROWS ONLY
Current dateCURRENT_DATECURDATE()GETDATE()

Anti-Patterns to Avoid

AvoidUse InsteadWhy
SELECT *Explicit column listBreaks code when schema changes
Implicit joins (FROM a, b WHERE ...)Explicit JOIN ONHard to read, error-prone
Functions on indexed columns in WHERESargable predicatesCan't use indexes
NOT IN with nullable columnsNOT EXISTS or LEFT JOIN WHERE NULLNULL handling issues
OR in JOIN conditionsSeparate queries with UNIONPoor performance

Performance Tips

  • Use sargable predicates: WHERE date >= '2024-01-01' not WHERE YEAR(date) = 2024
  • Avoid type casting in filters: WHERE id = 123 not WHERE id::text = '123'
  • Filter early, join late — reduce row count before expensive operations
  • Use EXISTS over IN for correlated checks on large sets

source: PostgreSQL docs, MySQL docs, SQL Server docs, SQL standards

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.