Skip to content
v0.1.0latest release21 skills2 categories3 sub-categories22 tested scripts68 reference docsContact us

SQL Quick Reference

Reference for sql-query-writer · 129 lines

Programming / SQLv1.0.0Updated OCT 20 20262 references1 script

Dialect support, clause order, JOIN types, operators, best practices and the validation checklist to run before returning any SQL.

Supported Database Dialects

DialectVersion SupportKey Differences
PostgreSQL12+Rich functions, JSONB, arrays, CTEs
MySQL8.0+AUTO_INCREMENT, backticks, LIMIT syntax
SQL Server2016+TOP, IDENTITY, T-SQL extensions
Oracle19c+ROWNUM, sequences, PL/SQL
SQLite3.35+Lightweight, limited types, file-based

Query Structure Reference

SELECT Statement Components

sql
SELECT [DISTINCT] columns
FROM table
[JOIN other_table ON condition]
[WHERE filter_condition]
[GROUP BY grouping_columns]
[HAVING aggregate_condition]
[ORDER BY sort_columns]
[LIMIT n OFFSET m]

Common Clause Order

  1. SELECT - What columns to return
  2. FROM - Source table(s)
  3. JOIN - Additional tables with relationships
  4. WHERE - Row-level filtering (before grouping)
  5. GROUP BY - Aggregate grouping
  6. HAVING - Aggregate filtering (after grouping)
  7. ORDER BY - Result sorting
  8. LIMIT/OFFSET - Pagination

Best Practices Summary

Query Writing

  • Use explicit column names, avoid SELECT *
  • Alias tables and complex expressions
  • Use parameterized queries to prevent SQL injection
  • Format queries for readability

Performance

  • Index columns used in WHERE, JOIN, ORDER BY
  • Avoid functions on indexed columns in WHERE
  • Use EXISTS instead of IN for subqueries
  • Limit result sets with appropriate filtering

Maintainability

  • Use CTEs for complex multi-step queries
  • Add meaningful aliases
  • Break complex logic into views or functions
  • Document business logic in comments

Error Handling Pattern

sql
-- PostgreSQL
BEGIN;
    -- your queries here
    IF condition THEN
        RAISE EXCEPTION 'Error message';
    END IF;
COMMIT;

-- SQL Server
BEGIN TRY
    BEGIN TRANSACTION;
    -- your queries here
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;
    THROW;
END CATCH;

Quick Reference Cards

Aggregation Functions

FunctionPurpose
COUNT(*)Total rows
COUNT(col)Non-NULL values
SUM(col)Total of values
AVG(col)Average value
MIN(col)Minimum value
MAX(col)Maximum value
GROUP_CONCAT / STRING_AGGConcatenate values

JOIN Types

TypeReturns
INNER JOINMatching rows only
LEFT JOINAll left + matching right
RIGHT JOINAll right + matching left
FULL OUTER JOINAll rows from both
CROSS JOINCartesian product

Common Operators

OperatorUsage
=Exact match
<> or !=Not equal
LIKEPattern match
INValue in list
BETWEENRange inclusive
IS NULLNULL check
EXISTSSubquery has results

Validation Checklist

Before outputting any SQL:

  • Syntax is valid for target dialect
  • Table and column names exist
  • JOIN conditions are complete (no cartesian products)
  • WHERE clause has appropriate filters
  • GROUP BY includes all non-aggregated columns
  • ORDER BY columns are in SELECT or valid
  • Parameters are properly escaped/parameterized
  • Query handles NULL values appropriately
More in SQL