sql-transformations
Skill2 starsChanged 10 months agoMITView on GitHub
ETL patterns, data cleaning, type conversions, and string/date manipulation for data transformations. Use this skill when building data pipelines, cleaning messy data, standardizing formats, or performing complex transformations. Covers data quality patterns, type conversions, string operations, date/time handling, NULL strategies, and deduplication.
In this repository38 other files it gives its agents.
- Data Engineering Cloud InfrastructureSkill
- data-eng-data-qualitySkill
- data-eng-streaming-patternsSkill
- data-eng-testing-patternsSkill
- data-eng-warehouse-patternsSkill
- DevOps CI/CD PatternsSkill
- devops-docker-patternsSkill
- devops-monitoring-observabilitySkill
- devops-terraform-patternsSkill
- git-advanced-operationsSkill
- git-workflow-patternsSkill
- modern-python-patternsSkill
- python-api-developmentSkill
- python-async-patternsSkill
- python-data-pipelinesSkill
- python-data-wranglingSkill
- python-devops-automationSkill
- python-error-handlingSkill
- python-package-developmentSkill
- python-performanceSkill
- python-testingSkill
- customizing-vectors-rSkill
- designing-oop-rSkill
- developing-packages-rSkill
- metaprogramming-rlangSkill
- optimizing-rSkill
- writing-tidyverse-rSkill
- sql-advanced-featuresSkill
- sql-analytics-patternsSkill
- sql-data-modelingSkill
- SQL Integration PatternsSkill
- sql-query-fundamentalsSkill
- sql-query-optimizationSkill
- API DesignSkill
- webdev-html-css-patternsSkill
- webdev-javascript-fundamentalsSkill
- webdev-react-patternsSkill
- webdev-typescript-patternsSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 2 are distinctive, and 1 is 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.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 19
Handle NULLs explicitly: - Never assume NULL behavior
- Distinctive: line 202
Use batch updates over row-by-row processing
- Very specific: line 203
Prefer EXISTS over IN for existence checks against large tables
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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.
Files
SQL Transformations
Essential SQL patterns for ETL, data cleaning, and transformation workflows.
Core Principles
- Clean at the source - Fix data quality issues as early as possible
- Idempotent transformations - Same input always produces same output
- Document assumptions - Make data quality rules explicit
- Handle NULLs explicitly - Never assume NULL behavior
- Test transformations - Validate with sample data before production
Data Cleaning Strategies
WITH
valid_records AS (
SELECT * FROM raw_customer_data
WHERE email IS NOT NULL AND email LIKE '%@%'
),
standardized AS (
SELECT
customer_id,
TRIM(UPPER(email)) AS email,
CASE
WHEN UPPER(country) IN ('US', 'USA', 'UNITED STATES') THEN 'United States'
WHEN UPPER(country) IN ('UK', 'UNITED KINGDOM', 'GB') THEN 'United Kingdom'
ELSE INITCAP(TRIM(country))
END AS country,
REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS phone_digits_only
FROM valid_records
)
SELECT * FROM standardized;See data-cleaning-patterns.md for:
- Validation rules and quality checks
- Standardization patterns and multi-stage pipelines
- Outlier detection and data quality scoring
Type Conversions
SELECT
-- Safe string to numeric
CASE
WHEN price_str ~ '^[0-9]+\.?[0-9]*$' THEN price_str::NUMERIC
ELSE NULL
END AS price,
-- Safe string to date
CASE
WHEN date_str ~ '^\d{4}-\d{2}-\d{2}$' THEN date_str::DATE
ELSE NULL
END AS order_date,
-- JSON extraction
(json_data->>'customer_id')::INTEGER AS customer_id
FROM staging_data;See type-conversions.md for:
- Safe type casting patterns across databases
- JSON and array handling
- Cross-database conversion syntax
String Operations
SELECT
INITCAP(product_name) AS product_title_case,
LPAD(id::TEXT, 10, '0') AS id_padded,
SPLIT_PART(email, '@', 2) AS email_domain,
REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS phone_numbers_only,
CONCAT_WS(', ', city, state, country) AS location
FROM products;See string-operations.md for:
- SUBSTRING, SPLIT_PART, string parsing
- REGEXP_REPLACE for pattern matching
- Text cleaning and normalization
Date and Time Operations
SELECT
order_date,
order_date + INTERVAL '7 days' AS delivery_date,
EXTRACT(YEAR FROM order_date) AS order_year,
DATE_TRUNC('month', order_date) AS order_month_start,
TO_CHAR(order_date, 'YYYY-MM-DD') AS date_iso,
DATE_PART('year', AGE(CURRENT_DATE, birth_date)) AS age_years
FROM orders;See date-time-operations.md for:
- Date arithmetic across databases
- Time zone handling
- Business day calculations
NULL Handling Strategies
SELECT
COALESCE(phone_mobile, phone_home, phone_work, 'No Phone') AS primary_phone,
NULLIF(TRIM(notes), '') AS notes_cleaned,
COALESCE(price, 0) * COALESCE(quantity, 0) AS total,
CASE
WHEN email IS NULL THEN 'missing'
WHEN email = '' THEN 'empty'
ELSE 'valid'
END AS email_status
FROM customers;See null-handling.md for:
- COALESCE vs NULLIF patterns
- NULL in aggregations and sorting
- Default value strategies
Deduplication Strategies
WITH ranked AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_date DESC, customer_id DESC
) AS rn
FROM customers
)
SELECT * FROM ranked WHERE rn = 1;See deduplication-strategies.md for:
- Finding and merging duplicate records
- DISTINCT ON (PostgreSQL)
- Deduplication with aggregation
Common Transformation Patterns
-- Pivot (rows to columns)
SELECT
product_id,
MAX(CASE WHEN attr = 'color' THEN value END) AS color,
MAX(CASE WHEN attr = 'size' THEN value END) AS size
FROM product_attributes
GROUP BY product_id;
-- Unpivot (columns to rows)
SELECT product_id, 'Q1' AS quarter, q1_sales AS sales FROM quarterly_sales
UNION ALL
SELECT product_id, 'Q2', q2_sales FROM quarterly_sales;ETL Best Practices
-- Idempotent upsert pattern
INSERT INTO target_table (id, name, value, updated_at)
SELECT id, name, value, CURRENT_TIMESTAMP
FROM source_table
ON CONFLICT (id)
DO UPDATE SET
name = EXCLUDED.name,
value = EXCLUDED.value,
updated_at = CURRENT_TIMESTAMP;Cross-Database Compatibility
| Feature | PostgreSQL | MySQL | SQL Server |
| String split | STRING_TO_ARRAY() | SUBSTRING_INDEX() | STRING_SPLIT() |
| Regex replace | REGEXP_REPLACE() | REGEXP_REPLACE() (8.0+) | N/A (use CLR) |
| Group concat | STRING_AGG() | GROUP_CONCAT() | STRING_AGG() (2017+) |
| JSON extract | ->> operator | JSON_EXTRACT() | JSON_VALUE() |
Anti-Patterns to Avoid
| Avoid | Use Instead | Why |
UPDATE without WHERE | Always specify conditions | Data corruption risk |
| Hardcoded dates | Parameterized or relative dates | Maintenance nightmare |
Multiple REPLACE calls | REGEXP_REPLACE | More efficient |
| Implicit NULL conversion | Explicit COALESCE | Predictable behavior |
DISTINCT to fix duplicates | Investigate root cause | May hide data quality issues |
Performance Tips
- Filter early, transform late — apply WHERE before expensive string/date operations
- Use batch updates over row-by-row processing
- Prefer
EXISTSoverINfor existence checks against large tables - Index columns used in JOIN and WHERE before running ETL
source: PostgreSQL docs, dbt best practices, Kimball ETL patterns
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
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.

