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
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 first_column,
second_column,
third_column
FROM table_name
WHERE first_condition
AND second_condition
AND third_condition;
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 active_employees AS (
SELECT employee_id,
first_name,
last_name
FROM employees
WHERE status = 'ACTIVE'
),
department_summary AS (
SELECT department_id,
COUNT(*) AS employee_count
FROM active_employees
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.status = 'ACTIVE'
AND d.status = 'ACTIVE';
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 < 50000
THEN 'Low'
WHEN salary BETWEEN 50000 AND 100000
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 (
1001,
'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 = 75000
WHERE employee_id = 1001;
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
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.
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---5
6# SQL Code Formatter
7
8Format, polish, and document SQL code following Oracle Database 19 best practices with consistent style and readability.
9
10## Purpose
11
12This 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.
13
14## When to Use This Skill
15
16Use this skill when:
17
18- Formatting or beautifying SQL code
19- Working with .sql files that need polishing
20- Improving SQL query readability
21- Standardizing SQL code style across projects
22- Documenting complex SQL queries
23- Reviewing or refactoring existing SQL code
24- Converting unformatted SQL to well-structured queries
25
26## Core Formatting Principles
27
28### 1. Case Conventions
29
30- **SQL Keywords**: UPPERCASE (SELECT, FROM, WHERE, JOIN, etc.)
31- **Identifiers**: lowercase (column names, table names, aliases)
32- **Consistency**: Maintain consistent casing throughout queries
33
34Example:
35```sql
36SELECT employee_id,
37 first_name,
38 last_name
39 FROM employees
40 WHERE department_id = 10;
41```
42
43### 2. Indentation and Alignment
44
45- Use **4 spaces** for indentation (no tabs)
46- Align subsequent columns/conditions vertically with the first item
47- Indent sub-queries one level deeper than parent query
48- Align clause keywords (SELECT, FROM, WHERE) at consistent positions
49
50### 3. Line Breaks and Structure
51
52- **First item on same line**: Start first column/condition on the same line as the clause keyword
53- **New line for each item**: Each subsequent column, condition, or table goes on a new line
54- **New line for clauses**: Start each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING) on a new line
55- **Vertical alignment**: Align continuation items vertically
56
57Example:
58```sql
59SELECT first_column,
60 second_column,
61 third_column
62 FROM table_name
63 WHERE first_condition
64 AND second_condition
65 AND third_condition;
66```
67
68### 4. Operators and Spacing
69
70- Single space on either side of operators (=, <, >, <=, >=, !=, ||, +, -, *, /)
71- Single space after commas
72- Single space around AS keyword for aliases
73
74### 5. Common Table Expressions (CTEs)
75
76- Begin with WITH keyword followed by CTE name
77- Place AS ( on the same line as CTE name
78- Close with ) on new line, aligned with WITH
79- Separate multiple CTEs with comma and line break
80
81Example:
82```sql
83WITH active_employees AS (
84 SELECT employee_id,
85 first_name,
86 last_name
87 FROM employees
88 WHERE status = 'ACTIVE'
89),
90department_summary AS (
91 SELECT department_id,
92 COUNT(*) AS employee_count
93 FROM active_employees
94 GROUP BY department_id
95)
96SELECT *
97 FROM department_summary;
98```
99
100### 6. JOIN Clauses
101
102- Explicitly specify JOIN type (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN)
103- Place JOIN and first ON condition on same line
104- Indent JOIN to align with FROM clause
105- Additional ON conditions go on new lines with AND keyword
106
107Example:
108```sql
109SELECT e.employee_id,
110 e.first_name,
111 d.department_name
112 FROM employees e
113 INNER JOIN departments d ON e.department_id = d.department_id
114 AND e.status = 'ACTIVE'
115 AND d.status = 'ACTIVE';
116```
117
118### 7. CASE Expressions
119
120- Start CASE with first WHEN on same line
121- Each subsequent WHEN, all THEN, and ELSE on separate lines
122- Align WHEN, THEN, and ELSE vertically
123- Place END aligned with CASE
124- Column alias on same line as END
125
126Example:
127```sql
128SELECT CASE WHEN salary < 50000
129 THEN 'Low'
130 WHEN salary BETWEEN 50000 AND 100000
131 THEN 'Medium'
132 ELSE 'High'
133 END AS salary_category
134 FROM employees;
135```
136
137### 8. INSERT Statements
138
139- List columns in parentheses, one per line (except first)
140- Align columns vertically
141- VALUES clause follows same pattern
142
143Example:
144```sql
145INSERT INTO employees (
146 employee_id,
147 first_name,
148 last_name,
149 department_id
150) VALUES (
151 1001,
152 'Jane',
153 'Smith',
154 20
155);
156```
157
158### 9. UPDATE Statements
159
160- Place table name on same line or new line after UPDATE
161- SET clause on new line
162- Each column assignment on new line (except first)
163- WHERE clause on new line
164
165Example:
166```sql
167UPDATE employees
168 SET first_name = 'John',
169 last_name = 'Doe',
170 salary = 75000
171 WHERE employee_id = 1001;
172```
173
174### 10. Comments
175
176- Use `--` for single-line comments
177- Use `/* comment */` for multi-line comments
178- Add comments to explain complex logic only when explicitly requested
179- Place comments above the code they describe
180
181## Bundled Resources
182
183### References (`references/`)
184
185- `references/sql-formatting-rules.md` - Complete formatting specification with all 13 rules and detailed examples
186
187Load this reference when working with complex SQL formatting scenarios or when users need detailed rule explanations.
188
189## How to Use This Skill
190
191### Basic SQL Formatting
192
193When user provides unformatted SQL code:
194
1951. Identify SQL statement type (SELECT, INSERT, UPDATE, DELETE, CREATE, etc.)
1962. Apply core formatting principles from this skill
1973. Ensure keywords are UPPERCASE and identifiers are lowercase
1984. Apply proper indentation (4 spaces)
1995. Align columns, conditions, and clauses vertically
2006. Return formatted SQL code
201
202### Advanced Formatting
203
204For complex queries with CTEs, joins, subqueries, and CASE expressions:
205
2061. Read `references/sql-formatting-rules.md` for detailed specifications
2072. Apply all 13 formatting rules in sequence
2083. Pay special attention to vertical alignment
2094. Ensure consistent indentation at all nesting levels
2105. Validate that all examples in the reference are followed
211
212### Working with .sql Files
213
214When user requests formatting of .sql files:
215
2161. Read the SQL file content
2172. Apply formatting rules to each statement
2183. Preserve existing comments unless reformatting is requested
2194. Write back formatted SQL to the file or display for review
2205. Ensure file encoding is preserved (UTF-8 recommended)
221
222## Key Information
223
224- **Database:** Oracle Database 19
225- **Indentation:** 4 spaces (no tabs)
226- **Keywords:** UPPERCASE
227- **Identifiers:** lowercase
228- **Line Length:** No strict limit, but prefer readability
229- **File Extension:** .sql
230
231## Best Practices
232
233- Apply formatting consistently across all SQL files in a project
234- Format SQL before committing to version control
235- Use vertical alignment to improve readability
236- Keep related conditions grouped with parentheses
237- Add comments sparingly, only for complex logic
238- Test formatted SQL to ensure functionality is preserved
239- Preserve the logical structure and query optimization
240
241## Troubleshooting
242
243**Issue: Query becomes too long horizontally**
244- Break long expressions across multiple lines
245- Use CTEs to simplify complex subqueries
246- Split long CASE expressions into multiple lines
247
248**Issue: Unclear which columns belong to which clause**
249- Ensure consistent vertical alignment
250- Use proper indentation (4 spaces per level)
251- Verify first column/condition is on same line as clause keyword
252
253**Issue: Complex joins are hard to read**
254- Place each JOIN on its own line
255- Align all JOINs with FROM clause
256- Put additional ON conditions on separate lines with AND
257
258**Issue: Formatted SQL doesn't execute**
259- Verify formatting didn't introduce syntax errors
260- Check that all parentheses are balanced
261- Ensure string literals are properly quoted
262- Test the query after formatting
263
264## Examples
265
266See the `examples/` directory for sample SQL files showing:
267
268- `examples/unformatted.sql` - Before formatting
269- `examples/formatted.sql` - After applying formatting rules
270- `examples/complex-query.sql` - Complex query with CTEs and joins
271
272## Additional Notes
273
274- This skill focuses on formatting and style, not query optimization
275- The formatting rules preserve Oracle SQL syntax and semantics
276- For very large SQL files (>1000 lines), consider formatting sections separately
277- Formatted SQL is easier to review, debug, and maintain
278- Consistent formatting improves team collaboration and code reviews