SQL in applications and data platforms
Structured Query Language expresses relational operations declaratively: you describe the result set or change you want, and the engine chooses join order, index use, and parallelism. SELECT reads rows through projections, filters, grouping, and ordering; INSERT, UPDATE, DELETE, and MERGE mutate data; DDL statements define schemas, constraints, and privileges.
Every vendor implements a dialect on top of shared grammar—MySQL backticks, PostgreSQL double-colon casts, T-SQL bracket identifiers, Snowflake stages, and PL/SQL blocks differ in lexing even when the business logic looks identical. Readable layout helps reviewers follow clause boundaries; it is not a substitute for EXPLAIN, statistics, or migration testing on the target engine.
SQL language — background
Statement families
- Query statements (SELECT and variants with CTEs or subqueries) return row sets without committing changes unless wrapped in a transaction with locks.
- DML changes existing rows—INSERT adds, UPDATE sets columns, DELETE removes, MERGE upserts according to match rules.
- DDL defines structure—CREATE/ALTER/DROP for tables, views, indexes, and roles; effects are metadata and may require locks or online rebuilds.
- Session and procedural constructs (BEGIN, DECLARE, cursors, dynamic SQL) belong to dialect-specific blocks that parsers treat separately from a bare SELECT.
Typical SELECT clause order
- FROM (and JOIN … ON) binds base relations; comma joins and explicit JOINs differ in precedence and should be written clearly.
- WHERE filters rows before grouping; predicates on non-aggregated columns live here.
- GROUP BY collapses rows; HAVING filters groups after aggregation.
- SELECT projects expressions; ORDER BY sorts the final row set; LIMIT/OFFSET or TOP/FETCH cap rows per dialect.
Dialect levers that affect parsing
- Identifier quoting: double quotes (ANSI), backticks (MySQL), or brackets (T-SQL) disambiguate reserved words and mixed-case names.
- String literals use single quotes in most engines; escape rules and dollar-quoting (PostgreSQL) change how embedded quotes tokenize.
- Vendor functions, :: casts, JSON operators, and window clauses (OVER PARTITION BY) must match the engine grammar to parse cleanly.
- Multi-statement scripts and procedural keywords fail generic parsers when the dialect preset does not include them.
Transactions, isolation, and what the engine guarantees
- ACID groups durability (commit survives crash), atomicity (all-or-nothing), isolation (concurrent sessions), and consistency (constraints hold at commit).
- Isolation levels trade phantom/non-repeatable reads for throughput: READ COMMITTED, REPEATABLE READ, and SERIALIZABLE behave differently per engine.
- MVCC keeps row versions so readers avoid blocking writers; writers may still conflict on the same logical row.
- EXPLAIN shows a chosen plan (nested loop, hash join, index scan)—not the SQL text layout on the page.
Where readable SQL matters
Incident and ORM log review
ORMs and connection pools often emit one-line SQL in traces. Indentation exposes join order, missing predicates, and accidental cartesian products during outages without running against production.
Migrations and warehouse analytics
Flyway/Liquibase scripts and ad hoc warehouse queries mix CTEs with window functions. Consistent clause breaks help DBAs compare revisions in code review even when engines differ.