Skip to content
WebDevTools

SQL Cheatsheet

Searchable SQL cheatsheet: queries, joins with diagrams, aggregation, window functions, CTEs, DDL and indexes. PostgreSQL, MySQL, SQLite.

Runs locally in your browser
110 entries

Querying data

  • Select all columns from a table

  • Select columns with an alias

  • Unique values only

  • Sort by multiple columns

  • Pagination (skip 20, take 10)

    PostgreSQLMySQLSQLite
  • Standard SQL pagination

    PostgreSQLSQL Server
  • First N rows

    SQL Server
  • Conditional expression

  • First non-NULL value

  • Return NULL if values are equal (avoid division by zero)

Filtering

  • Combine conditions

  • Match any value in a list

  • Inclusive range

  • Pattern match (% any chars, _ one char)

  • Case-insensitive LIKE

    PostgreSQL
  • Test for NULL (never use = NULL)

  • Rows that have related rows

  • Regular-expression match

    PostgreSQL
  • Regular-expression match

    MySQL

Joins

  • Only rows that match in both tables

    AB
  • All rows from A, matching rows from B (NULL if none)

    AB
  • All rows from B, matching rows from A

    AB
  • All rows from both tables (not in MySQL: UNION a LEFT and RIGHT join)

    PostgreSQLSQL ServerSQLite
    AB
  • Rows in A with no match in B (anti-join)

    AB
  • Every combination of rows (Cartesian product)

    AB
  • Self join

  • Join on same-named columns

  • Top-N per group with a lateral join

    PostgreSQLMySQL

Aggregation

  • Count rows

  • Count unique values

  • Common aggregate functions

  • Aggregate per group

  • Filter groups after aggregation

  • Concatenate values of a group

    PostgreSQLSQL Server
  • Concatenate values of a group

    MySQL
  • Conditional aggregate

    PostgreSQLSQLite
  • Conditional aggregate (portable)

  • Subtotals and a grand total

    PostgreSQLMySQLSQL Server

Window functions

  • Number rows within each group

  • Rank with gaps on ties

  • Rank without gaps

  • Running total

  • 7-row moving average

  • Value from the previous row

  • Value from the next row

  • First value in the window

  • Split rows into N buckets (quartiles)

  • Latest row per group

Subqueries & CTEs

  • Subquery in WHERE

  • Correlated scalar subquery

  • Common table expression (CTE)

  • Recursive CTE (walk a hierarchy)

  • Combine results, removing duplicates

  • Combine results, keeping duplicates (faster)

  • Rows present in both results

  • Rows in the first result but not the second

Modifying data

  • Insert a row

  • Insert multiple rows

  • Insert from a query

  • Update matching rows

  • Delete matching rows

  • Remove all rows quickly

  • Upsert

    PostgreSQLSQLite
  • Upsert

    MySQL
  • Standard MERGE (upsert)

    PostgreSQLSQL Server
  • Return modified rows

    PostgreSQLSQLite

Tables (DDL)

  • Create a table with identity key and defaults

    PostgreSQL
  • Create a table with auto-increment key

    MySQL
  • Create only if missing

  • Add a column

  • Remove a column

  • Rename a column

  • Add a foreign key

  • Add a check constraint

  • Delete a table

  • Create a view

Indexes & performance

  • Create an index

  • Unique expression index

    PostgreSQLSQLite
  • Composite index (column order matters)

  • Partial index

    PostgreSQLSQLite
  • Build an index without locking writes

    PostgreSQL
  • Remove an index

  • Show the execution plan with real timings

    PostgreSQLMySQL
  • Show the query plan

    SQLite
  • Update planner statistics

Transactions

  • Run statements atomically

  • Undo the current transaction

  • Partial rollback inside a transaction

  • Strictest isolation level

  • Lock selected rows until commit

    PostgreSQLMySQL
  • Queue pattern: grab an unlocked row

    PostgreSQLMySQL

Strings, dates & JSON

  • Concatenate strings

  • Concatenate with the standard operator

    PostgreSQLSQLite
  • Common string functions

  • Substring

  • Replace text

  • Convert a type

  • Current timestamp and date

  • Truncate to month

    PostgreSQL
  • Date arithmetic

    PostgreSQL
  • Date arithmetic

    MySQL
  • Extract part of a date

  • Read a JSON field / containment

    PostgreSQL
  • Read a JSON field

    MySQLSQLite

Users & admin

  • Create a database user

    PostgreSQL
  • Grant privileges

  • Revoke privileges

  • psql: list tables, describe table, list DBs, connect

    PostgreSQL
  • MySQL: list tables, describe table, list DBs

    MySQL
  • sqlite3 CLI: list tables, show schema

    SQLite

About this SQL cheatsheet

A compact SQL reference covering querying, joins, aggregation, window functions, CTEs, data modification, schema changes, indexes and transactions.

Most statements are standard SQL and work everywhere. When syntax differs between databases, the entry is tagged with the dialects that support it: PostgreSQL, MySQL, SQLite or SQL Server.

How to use it: search for a keyword (e.g. upsert, rank, json) or pick a section. Click a statement to copy it.