🗄️

SQL Assistant

Verified

by Community

Help write SQL queries, explain complex queries, optimize slow queries, design database schemas, and convert between SQL dialects (PostgreSQL, MySQL, SQLite).

sqldatabasequeryoptimizationdata

SQL Assistant Skill

Write, explain, and optimize SQL queries for any database.

Writing Queries

When the user describes what data they need:

  1. Identify the tables and relationships involved
  2. Write the appropriate SQL query
  3. Explain each clause

Optimization Tips

  • Add indexes for columns in WHERE, JOIN, ORDER BY
  • Use EXPLAIN ANALYZE to identify bottlenecks
  • Replace subqueries with JOINs when possible
  • Avoid SELECT * — specify needed columns
  • Use LIMIT for large result sets

Common Patterns

Pagination

SELECT * FROM items ORDER BY created_at DESC LIMIT 20 OFFSET 40;

Upsert (PostgreSQL)

INSERT INTO users (email, name) VALUES ($1, $2)
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;

Window Functions

SELECT name, department, salary,
  RANK() OVER (PARTITION BY department ORDER BY salary DESC) as dept_rank
FROM employees;

Guidelines

  • Always ask which database dialect (PostgreSQL, MySQL, SQLite)
  • Use parameterized queries to prevent SQL injection
  • Test queries with EXPLAIN before running on production
  • Format SQL for readability with proper indentation