Web Utility Desk
Blog Guides JSON URL Resources Search

Resources ยท Cheat Sheets

SQL Query Syntax & Formatting Cheat Sheet

Overview

Structured Query Language (SQL) is the standard language for querying, manipulating, and analyzing relational databases. As queries grow across multi-table JOINs, subqueries, and window functions, unformatted SQL becomes nearly impossible to read or debug safely.

This cheat sheet aggregates standard syntax conventions, clause ordering rules, JOIN behavior tables, practical examples, and team formatting guidelines across major dialects including PostgreSQL, MySQL, and SQL Server.

Syntax Tables: Standard Query Ordering & JOINs

Every SQL query executes in a strict logical order that differs from written syntax. Understanding standard clause structure prevents grouping and alias bugs:

Clause Logical Order Purpose & Formatting Rule
`SELECT` 5th Defines returned columns. Place each column on a new line when querying more than 2 fields.
`FROM` / `JOIN` 1st Identifies source tables and joining relationships. Align `ON` conditions explicitly.
`WHERE` 2nd Filters raw rows before grouping. Cannot use aggregate functions here.
`GROUP BY` 3rd Aggregates rows into groups for functions like `COUNT()`, `SUM()`, or `AVG()`.
`HAVING` 4th Filters grouped summary records post-aggregation.
`ORDER BY` / `LIMIT` 6th Sorts final results and restricts row count returned to client.

Practical Formatting Example

Below is a production-style query structured for maximum reviewability across engineering teams:

SELECT
  u.id AS user_id,
  u.email,
  COUNT(o.id) AS total_orders,
  SUM(o.amount) AS lifetime_value
FROM users AS u
LEFT JOIN orders AS o
  ON u.id = o.user_id
  AND o.status = 'completed'
WHERE u.created_at >= '2026-01-01'
GROUP BY
  u.id,
  u.email
HAVING SUM(o.amount) > 500.00
ORDER BY lifetime_value DESC
LIMIT 50;

Common Mistakes & How to Avoid Them

  • Using `SELECT *` in Production: Wildcard selects increase network overhead and break applications when underlying table schemas mutate. Always enumerate required columns explicitly.
  • Filtering Aggregates in `WHERE`: Writing `WHERE COUNT(id) > 5` throws an syntax error. Use `HAVING` for filtering aggregated metric results.
  • Inconsistent Casing: Mixing uppercase and lowercase keywords (`select id from Users where...`) makes queries harder to scan during incidents. Standardize on uppercase keywords.

Formatting Best Practices

  • Uppercase Keywords: Write reserved commands (`SELECT`, `JOIN`, `WHERE`) in uppercase and table/column names in lowercase `snake_case`.
  • Leading vs Trailing Commas: Most teams prefer trailing commas at line endings for readability, though leading commas are occasionally used to simplify git diffs. Pick one style per repo.
  • Explicit Aliases: Always use `AS` when aliasing tables and columns (`users AS u`) rather than relying on implicit space separators (`users u`).

Related Interactive Tools

SF

SQL Formatter

Instantly format, indent, and standardize raw SQL queries across database dialects.

Open tool
DIFF

Text Diff Checker

Compare two versions of complex queries side by side to review logic and clause mutations.

Open tool

Related Engineering Guides

SQL

SQL Formatting Rules

Deep dive into team standards for indentation, line breaks, alias rules, and CTE structure.

Read guide
RD

How to Read Complex SQL Queries

Learn a systematic mental framework for deconstructing legacy subqueries and window functions.

Read guide

Frequently Asked Questions

Why should SQL keywords always be capitalized?

Capitalizing reserved keywords (SELECT, FROM, WHERE, JOIN) creates clear visual separation between query operations and custom database table/column identifiers.

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows where the join condition matches in both tables. LEFT JOIN returns all rows from the left table, with NULL values for un-matched columns from the right table.

How can I format complex un-indented SQL queries automatically?

Paste your raw query string into our interactive browser-based SQL Formatter to automatically enforce standard indentation, keyword casing, and line wrapping.

Web Utility Desk

Free web utility tools for everyday technical work.

Tools JSON Formatter JSON Validator CSV to JSON Converter Regex Tester Regex Generator SQL Formatter JSON Compare Tool HTML Minifier CSS Minifier JavaScript Minifier
Site About Blog Guides Resources Contact Privacy Terms