SQL Templates: CRUD Skeletons, JOIN Types and Three Pitfalls

Open Tool SQL Cheat Sheet

What the templates cover

They follow the four CRUD lines: SELECT for reading, INSERT for writing, UPDATE for modifying, DELETE for removing, then the refinements of WHERE filters, JOIN across tables and GROUP BY aggregation. Each shows a skeleton with placeholders for table, column and condition. The value is fixing the syntax frame so you stop omitting FROM, forgetting ON or misspelling WHERE.

Steps

  1. Pick a statement type on the left, such as JOIN or GROUP BY.
  2. Read the skeleton, parameter notes and sample output, then Copy the code.
  3. Load example fills a full statement set; Clear resets.

Four JOIN types

INNER JOIN keeps only rows matched on both sides; LEFT JOIN keeps the whole left table and leaves right columns null when unmatched; RIGHT JOIN mirrors that; FULL OUTER JOIN keeps both. MySQL lacks FULL OUTER JOIN, so you pair LEFT and RIGHT under a UNION. Another subtlety: filtering a right-table column in WHERE after a LEFT JOIN demotes the outer join to an inner one, since nulls fail the test — keep the condition in ON instead. That is the most common logic error in practice.

Three rules for UPDATE and DELETE

First, write WHERE before SET, because an UPDATE without WHERE rewrites the entire table. Second, run a SELECT under the same WHERE to confirm the affected rows and targets before mutating. Third, back up or wrap in a transaction: MySQL DELETE rolls back, TRUNCATE does not and also resets the auto-increment value. Simple rules, yet most production incidents live here.

Dialect gaps and aggregation traps

Paging differs: LIMIT offset, size in MySQL, LIMIT size OFFSET offset in PostgreSQL, TOP or OFFSET FETCH in SQL Server. Concatenation uses CONCAT in MySQL and the plus sign in SQL Server. On aggregation, COUNT(*) counts rows while COUNT(column) skips nulls, which can diverge sharply; SUM returns null rather than zero over nulls, so wrap with COALESCE. And columns after GROUP BY must be grouped columns or aggregates, or strict mode rejects the query.

⏫︎