sql-expert
Write and optimize T-SQL queries for SQL Server
Also available from: AutumnsGrove
Get expert help writing complex T-SQL queries, stored procedures, and performance optimizations for Microsoft SQL Server. Covers advanced patterns like CTEs, window functions, PIVOT, and MERGE statements.
Download the skill ZIP
Upload in Claude
Go to Settings → Capabilities → Skills → Upload skill
Toggle on and start using
Test it
Using "sql-expert". Write a recursive CTE to traverse an organizational hierarchy where each employee has a manager_id pointing to another employee
Expected outcome:
- WITH RECURSIVE hierarchy AS (
- -- Anchor: top-level employees (no manager)
- SELECT id, name, manager_id, 0 AS level
- FROM employees WHERE manager_id IS NULL
- UNION ALL
- -- Recursive: employees reporting to current level
- SELECT e.id, e.name, e.manager_id, h.level + 1
- FROM employees e
- INNER JOIN hierarchy h ON e.manager_id = h.id
- )
- SELECT * FROM hierarchy OPTION (MAXRECURSION 100)
Using "sql-expert". How do I implement keyset pagination for better performance on large tables?
Expected outcome:
- Keyset (seek method) pagination is more efficient than OFFSET for large datasets:
- CREATE PROCEDURE GetProductsPage
- @LastProductId INT,
- @PageSize INT = 20
- AS
- BEGIN
- SELECT TOP (@PageSize) * FROM Products
- WHERE ProductId > @LastProductId
- ORDER BY ProductId;
- END
- Benefits: consistent performance at any page, works with real-time data
Security Audit
SafeThis is a documentation-only skill containing markdown files with T-SQL code examples. All 265 static findings are FALSE_POSITIVES. The analyzer incorrectly flags markdown syntax (triple backticks for code blocks) as shell commands, security education content as vulnerabilities, and SQL Server features like encryption as weak crypto. No executable code, network access, or file system operations exist.
Risk Factors
⚙️ External commands (152)
🌐 Network access (2)
Quality Score
What You Can Build
Write complex T-SQL queries
Create queries with advanced patterns like recursive CTEs, running totals, and gap detection
Optimize slow queries
Analyze query patterns and execution plans to identify and fix performance bottlenecks
Build secure stored procedures
Implement parameterized queries, proper error handling, and transaction management patterns
Try These Prompts
Write a T-SQL query to select all orders from last month with their customer names, joining the Orders and Customers tables
Create a query using window functions to show each product with its sales, running total, and rank within category
This query takes 30 seconds to run on a table with 10 million rows. Suggest optimizations including index changes and query rewrite: SELECT * FROM transactions WHERE YEAR(date) = 2024
Show me how to safely build a dynamic WHERE clause that accepts user input for filtering, preventing SQL injection while allowing optional filters
Best Practices
- Always use parameterized queries with sp_executesql instead of string concatenation to prevent SQL injection
- Write SARGable queries by avoiding functions on indexed columns in WHERE clauses
- Handle NULLs explicitly using IS NULL, ISNULL(), or COALESCE rather than relying on defaults
Avoid
- Using SELECT * in production queries - explicitly list columns to improve performance and avoid schema binding issues
- Calling functions on indexed columns in WHERE clauses (e.g., WHERE YEAR(date) = 2024) which prevents index usage
- Not validating or white-listing dynamic object names in dynamic SQL, even when using sp_executesql