coderef

SQL Quick Reference

SQL queries: SELECT, filtering, sorting, joins, grouping, inserts, updates, schema changes and transactions.

7 sections · 50 snippets · updated Oct 14, 2026

Click any snippet to copy it.

Querying

Reading rows with SELECT.

  • Select all columns
  • Select some columns
  • Rename a column
  • Unique values
  • Limit rows
  • Sort
  • Count rows

Filtering

Choosing rows with WHERE.

  • Equals / not equals
  • Comparison
  • AND / OR
  • In a list
  • Between
  • Pattern match
  • NULL checks
  • Subquery
  • Exists

Joins

Combining tables.

  • Inner join
  • Left join (keep all left rows)
  • Rows with no match
  • Full outer join
  • Self join
  • Combine result sets

Grouping & aggregates

Summarising data.

  • Aggregate functions
  • Group by
  • Filter groups
  • Conditional value
  • Default for NULL
  • Row number / ranking
  • Running total
  • Common table expression

Changing data

INSERT, UPDATE and DELETE.

  • Insert a row
  • Insert many rows
  • Insert from a query
  • Update rows
  • Delete rows
  • Upsert (PostgreSQL)
  • Return changed rows (PostgreSQL)

Schema

Creating and changing tables.

  • Create a table
  • Foreign key
  • Add a column
  • Rename / drop a column
  • Create an index
  • Drop a table
  • Empty a table
  • Create a view

Transactions & admin

Safety and inspection.

  • Transaction
  • Explain a query
  • Create a user (PostgreSQL)
  • Grant access
  • List tables

← All quick references