SQL Code Formatter
Format, polish, and document SQL code following Oracle Database 19 best practices with consistent style and readability.
Purpose
This skill provides comprehensive SQL code formatting rules and guidelines for Oracle Database 19. It enforces consistent code style, improves readability, and applies industry-standard formatting conventions to SQL queries, DDL statements, and DML operations.
When to Use This Skill
Use this skill when:
- Formatting or beautifying SQL code
- Working with .sql files that need polishing
- Improving SQL query readability
- Standardizing SQL code style across projects
- Documenting complex SQL queries
- Reviewing or refactoring existing SQL code
- Converting unformatted SQL to well-structured queries
Core Formatting Principles
These principles summarize the most common cases. The complete 13-rule specification lives in references/sql-formatting-rules.md; load it for full coverage and edge cases.
1. Case Conventions
- SQL Keywords: UPPERCASE (SELECT, FROM, WHERE, JOIN, etc.)
- Identifiers: lowercase (column names, table names, aliases)
- Consistency: Maintain consistent casing throughout queries
Example:
SELECT employee_id,
first_name,
last_name
FROM employees
WHERE department_id = 10;
2. Indentation and Alignment
- Use 4 spaces for indentation (no tabs)
- Align subsequent columns/conditions vertically with the first item
- Indent sub-queries one level deeper than parent query
- Align clause keywords (SELECT, FROM, WHERE) at consistent positions
3. Line Breaks and Structure
- First item on same line: Start first column/condition on the same line as the clause keyword
- New line for each item: Each subsequent column, condition, or table goes on a new line
- New line for clauses: Start each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING) on a new line
- Vertical alignment: Align continuation items vertically
Example:
SELECT employee_id,
first_name,
last_name
FROM employees
WHERE department_id = 50
AND salary > 5000
AND commission_pct IS NOT NULL;
4. Operators and Spacing
- Single space on either side of operators (=, <, >, <=, >=, !=, ||, +, -, *, /)
- Single space after commas
- Single space around AS keyword for aliases
5. Common Table Expressions (CTEs)
- Begin with WITH keyword followed by CTE name
- Place AS ( on the same line as CTE name
- Close with ) on new line, aligned with WITH
- Separate multiple CTEs with comma and line break
Example:
WITH high_earners AS (
SELECT employee_id,
first_name,
last_name,
department_id
FROM employees
WHERE salary > 5000
),
department_summary AS (
SELECT department_id,
COUNT(*) AS employee_count
FROM high_earners
GROUP BY department_id
)
SELECT *
FROM department_summary;
6. JOIN Clauses
- Explicitly specify JOIN type (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN)
- Place JOIN and first ON condition on same line
- Indent JOIN to align with FROM clause
- Additional ON conditions go on new lines with AND keyword
Example:
SELECT e.employee_id,
e.first_name,
d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
AND e.salary > 5000
AND d.location_id = 1700;
7. CASE Expressions
- Start CASE with first WHEN on same line
- Each subsequent WHEN, all THEN, and ELSE on separate lines
- Align WHEN, THEN, and ELSE vertically
- Place END aligned with CASE
- Column alias on same line as END
Example:
SELECT CASE WHEN salary < 5000
THEN 'Low'
WHEN salary BETWEEN 5000 AND 10000
THEN 'Medium'
ELSE 'High'
END AS salary_category
FROM employees;
8. INSERT Statements
- List columns in parentheses, one per line (except first)
- Align columns vertically
- VALUES clause follows same pattern
Example:
INSERT INTO employees (
employee_id,
first_name,
last_name,
department_id
) VALUES (
208,
'Jane',
'Smith',
20
);
9. UPDATE Statements
- Place table name on same line or new line after UPDATE
- SET clause on new line
- Each column assignment on new line (except first)
- WHERE clause on new line
Example:
UPDATE employees
SET first_name = 'John',
last_name = 'Doe',
salary = 9000
WHERE employee_id = 207;
10. Comments
- Use
-- for single-line comments
- Use
/* comment */ for multi-line comments
- Add comments to explain complex logic only when explicitly requested
- Place comments above the code they describe
11. Subqueries and Derived Tables
- Keep the opening
( on the same line as the clause keyword (for example, FROM ()
- Indent the subquery body one level deeper so it sits to the right of the
(, and align its clause keywords on their own river
- Place the closing
) on a new line, aligned with the opening ( (unlike CTEs, where ) aligns with WITH)
- A short subquery may stay on one line, such as
IN (SELECT department_id FROM departments)
Example:
SELECT COUNT(*)
FROM (
SELECT employee_id
FROM employees
WHERE salary > 5000
ORDER BY employee_id
);
Bundled Resources
References (references/)
references/sql-formatting-rules.md - Complete formatting specification with all 13 rules and detailed examples
Load this reference when working with complex SQL formatting scenarios or when users need detailed rule explanations.
Examples (examples/)
examples/unformatted.sql - Before-formatting samples
examples/formatted.sql - The same statements after the formatting rules are applied
examples/complex-query.sql - Comprehensive query exercising all 13 rules
Compare the unformatted and formatted files to demonstrate the rules in practice.
How to Use This Skill
Basic SQL Formatting
When user provides unformatted SQL code:
- Identify SQL statement type (SELECT, INSERT, UPDATE, DELETE, CREATE, etc.)
- Apply core formatting principles from this skill
- Ensure keywords are UPPERCASE and identifiers are lowercase
- Apply proper indentation (4 spaces)
- Align columns, conditions, and clauses vertically
- Return formatted SQL code
Advanced Formatting
For complex queries with CTEs, joins, subqueries, and CASE expressions:
- Read
references/sql-formatting-rules.md for detailed specifications
- Apply all 13 formatting rules in sequence
- Pay special attention to vertical alignment
- Ensure consistent indentation at all nesting levels
- Validate that all examples in the reference are followed
Working with .sql Files
When user requests formatting of .sql files:
- Read the SQL file content
- Apply formatting rules to each statement
- Preserve existing comments unless reformatting is requested
- Write back formatted SQL to the file or display for review
- Ensure file encoding is preserved (UTF-8 recommended)
Key Information
- Database: Oracle Database 19
- Indentation: 4 spaces (no tabs)
- Keywords: UPPERCASE
- Identifiers: lowercase
- Line Length: No strict limit, but prefer readability
- File Extension: .sql
Best Practices
- Apply formatting consistently across all SQL files in a project
- Format SQL before committing to version control
- Use vertical alignment to improve readability
- Keep related conditions grouped with parentheses
- Add comments sparingly, only for complex logic
- Test formatted SQL to ensure functionality is preserved
- Preserve the logical structure and query optimization
Troubleshooting
Issue: Query becomes too long horizontally
- Break long expressions across multiple lines
- Use CTEs to simplify complex subqueries
- Split long CASE expressions into multiple lines
Issue: Unclear which columns belong to which clause
- Ensure consistent vertical alignment
- Use proper indentation (4 spaces per level)
- Verify first column/condition is on same line as clause keyword
Issue: Complex joins are hard to read
- Place each JOIN on its own line
- Align all JOINs with FROM clause
- Put additional ON conditions on separate lines with AND
Issue: Formatted SQL doesn't execute
- Verify formatting didn't introduce syntax errors
- Check that all parentheses are balanced
- Ensure string literals are properly quoted
- Test the query after formatting
Examples
See the examples/ directory for sample SQL files showing:
examples/unformatted.sql - Before formatting
examples/formatted.sql - After applying formatting rules
examples/complex-query.sql - Complex query with CTEs and joins
Additional Notes
- This skill focuses on formatting and style, not query optimization
- The formatting rules preserve Oracle SQL syntax and semantics
- For very large SQL files (>1000 lines), consider formatting sections separately
- Formatted SQL is easier to review, debug, and maintain
- Consistent formatting improves team collaboration and code reviews
1---2name: sql-formatter3description: This skill should be used when the user asks to format SQL code, polish SQL queries, improve SQL readability, or work with .sql files. Use when queries mention SQL formatting, code beautification, Oracle SQL, or database query polishing.4---56# SQL Code Formatter78Format, polish, and document SQL code following Oracle Database 19 best practices with consistent style and readability.910## Purpose1112This skill provides comprehensive SQL code formatting rules and guidelines for Oracle Database 19. It enforces consistent code style, improves readability, and applies industry-standard formatting conventions to SQL queries, DDL statements, and DML operations.1314## When to Use This Skill1516Use this skill when:1718- Formatting or beautifying SQL code19- Working with .sql files that need polishing20- Improving SQL query readability21- Standardizing SQL code style across projects22- Documenting complex SQL queries23- Reviewing or refactoring existing SQL code24- Converting unformatted SQL to well-structured queries2526## Core Formatting Principles2728These principles summarize the most common cases. The complete 13-rule specification lives in `references/sql-formatting-rules.md`; load it for full coverage and edge cases.2930### 1. Case Conventions3132- **SQL Keywords**: UPPERCASE (SELECT, FROM, WHERE, JOIN, etc.)33- **Identifiers**: lowercase (column names, table names, aliases)34- **Consistency**: Maintain consistent casing throughout queries3536Example:3738```sql39SELECT employee_id,40 first_name,41 last_name42 FROM employees43 WHERE department_id = 10;44```4546### 2. Indentation and Alignment4748- Use **4 spaces** for indentation (no tabs)49- Align subsequent columns/conditions vertically with the first item50- Indent sub-queries one level deeper than parent query51- Align clause keywords (SELECT, FROM, WHERE) at consistent positions5253### 3. Line Breaks and Structure5455- **First item on same line**: Start first column/condition on the same line as the clause keyword56- **New line for each item**: Each subsequent column, condition, or table goes on a new line57- **New line for clauses**: Start each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING) on a new line58- **Vertical alignment**: Align continuation items vertically5960Example:6162```sql63SELECT employee_id,64 first_name,65 last_name66 FROM employees67 WHERE department_id = 5068 AND salary > 500069 AND commission_pct IS NOT NULL;70```7172### 4. Operators and Spacing7374- Single space on either side of operators (=, <, >, <=, >=, !=, ||, +, -, *, /)75- Single space after commas76- Single space around AS keyword for aliases7778### 5. Common Table Expressions (CTEs)7980- Begin with WITH keyword followed by CTE name81- Place AS ( on the same line as CTE name82- Close with ) on new line, aligned with WITH83- Separate multiple CTEs with comma and line break8485Example:8687```sql88WITH high_earners AS (89 SELECT employee_id,90 first_name,91 last_name,92 department_id93 FROM employees94 WHERE salary > 500095),96department_summary AS (97 SELECT department_id,98 COUNT(*) AS employee_count99 FROM high_earners100 GROUP BY department_id101)102SELECT *103 FROM department_summary;104```105106### 6. JOIN Clauses107108- Explicitly specify JOIN type (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN)109- Place JOIN and first ON condition on same line110- Indent JOIN to align with FROM clause111- Additional ON conditions go on new lines with AND keyword112113Example:114115```sql116SELECT e.employee_id,117 e.first_name,118 d.department_name119 FROM employees e120 INNER JOIN departments d ON e.department_id = d.department_id121 AND e.salary > 5000122 AND d.location_id = 1700;123```124125### 7. CASE Expressions126127- Start CASE with first WHEN on same line128- Each subsequent WHEN, all THEN, and ELSE on separate lines129- Align WHEN, THEN, and ELSE vertically130- Place END aligned with CASE131- Column alias on same line as END132133Example:134135```sql136SELECT CASE WHEN salary < 5000137 THEN 'Low'138 WHEN salary BETWEEN 5000 AND 10000139 THEN 'Medium'140 ELSE 'High'141 END AS salary_category142 FROM employees;143```144145### 8. INSERT Statements146147- List columns in parentheses, one per line (except first)148- Align columns vertically149- VALUES clause follows same pattern150151Example:152153```sql154INSERT INTO employees (155 employee_id,156 first_name,157 last_name,158 department_id159) VALUES (160 208,161 'Jane',162 'Smith',163 20164);165```166167### 9. UPDATE Statements168169- Place table name on same line or new line after UPDATE170- SET clause on new line171- Each column assignment on new line (except first)172- WHERE clause on new line173174Example:175176```sql177UPDATE employees178 SET first_name = 'John',179 last_name = 'Doe',180 salary = 9000181 WHERE employee_id = 207;182```183184### 10. Comments185186- Use `--` for single-line comments187- Use `/* comment */` for multi-line comments188- Add comments to explain complex logic only when explicitly requested189- Place comments above the code they describe190191### 11. Subqueries and Derived Tables192193- Keep the opening `(` on the same line as the clause keyword (for example, `FROM (`)194- Indent the subquery body one level deeper so it sits to the right of the `(`, and align its clause keywords on their own river195- Place the closing `)` on a new line, aligned with the opening `(` (unlike CTEs, where `)` aligns with `WITH`)196- A short subquery may stay on one line, such as `IN (SELECT department_id FROM departments)`197198Example:199200```sql201SELECT COUNT(*)202 FROM (203 SELECT employee_id204 FROM employees205 WHERE salary > 5000206 ORDER BY employee_id207 );208```209210## Bundled Resources211212### References (`references/`)213214- `references/sql-formatting-rules.md` - Complete formatting specification with all 13 rules and detailed examples215216Load this reference when working with complex SQL formatting scenarios or when users need detailed rule explanations.217218### Examples (`examples/`)219220- `examples/unformatted.sql` - Before-formatting samples221- `examples/formatted.sql` - The same statements after the formatting rules are applied222- `examples/complex-query.sql` - Comprehensive query exercising all 13 rules223224Compare the unformatted and formatted files to demonstrate the rules in practice.225226## How to Use This Skill227228### Basic SQL Formatting229230When user provides unformatted SQL code:2312321. Identify SQL statement type (SELECT, INSERT, UPDATE, DELETE, CREATE, etc.)2332. Apply core formatting principles from this skill2343. Ensure keywords are UPPERCASE and identifiers are lowercase2354. Apply proper indentation (4 spaces)2365. Align columns, conditions, and clauses vertically2376. Return formatted SQL code238239### Advanced Formatting240241For complex queries with CTEs, joins, subqueries, and CASE expressions:2422431. Read `references/sql-formatting-rules.md` for detailed specifications2442. Apply all 13 formatting rules in sequence2453. Pay special attention to vertical alignment2464. Ensure consistent indentation at all nesting levels2475. Validate that all examples in the reference are followed248249### Working with .sql Files250251When user requests formatting of .sql files:2522531. Read the SQL file content2542. Apply formatting rules to each statement2553. Preserve existing comments unless reformatting is requested2564. Write back formatted SQL to the file or display for review2575. Ensure file encoding is preserved (UTF-8 recommended)258259## Key Information260261- **Database:** Oracle Database 19262- **Indentation:** 4 spaces (no tabs)263- **Keywords:** UPPERCASE264- **Identifiers:** lowercase265- **Line Length:** No strict limit, but prefer readability266- **File Extension:** .sql267268## Best Practices269270- Apply formatting consistently across all SQL files in a project271- Format SQL before committing to version control272- Use vertical alignment to improve readability273- Keep related conditions grouped with parentheses274- Add comments sparingly, only for complex logic275- Test formatted SQL to ensure functionality is preserved276- Preserve the logical structure and query optimization277278## Troubleshooting279280### Issue: Query becomes too long horizontally281282- Break long expressions across multiple lines283- Use CTEs to simplify complex subqueries284- Split long CASE expressions into multiple lines285286### Issue: Unclear which columns belong to which clause287288- Ensure consistent vertical alignment289- Use proper indentation (4 spaces per level)290- Verify first column/condition is on same line as clause keyword291292### Issue: Complex joins are hard to read293294- Place each JOIN on its own line295- Align all JOINs with FROM clause296- Put additional ON conditions on separate lines with AND297298### Issue: Formatted SQL doesn't execute299300- Verify formatting didn't introduce syntax errors301- Check that all parentheses are balanced302- Ensure string literals are properly quoted303- Test the query after formatting304305## Examples306307See the `examples/` directory for sample SQL files showing:308309- `examples/unformatted.sql` - Before formatting310- `examples/formatted.sql` - After applying formatting rules311- `examples/complex-query.sql` - Complex query with CTEs and joins312313## Additional Notes314315- This skill focuses on formatting and style, not query optimization316- The formatting rules preserve Oracle SQL syntax and semantics317- For very large SQL files (>1000 lines), consider formatting sections separately318- Formatted SQL is easier to review, debug, and maintain319- Consistent formatting improves team collaboration and code reviews