Skip to main content
dbSyntax
Home CoursePracticeReferenceModelingInterview Prep
Cheatsheets
  • Cheat sheets
  • SQL cheat sheet
  • SQL joins cheat sheet
  • SQL window functions cheat sheet
  • SQL date functions cheat sheet
  • SQL string functions cheat sheet
  • PostgreSQL cheat sheet
  • MySQL cheat sheet
  • BigQuery cheat sheet
  • Snowflake cheat sheet
  • SQLite cheat sheet
  • SQL Server (T-SQL) cheat sheet
Start
  • SQL Reference
  • SQL Syntax Types
Query Structure
  • Query Structure
  • SELECT
  • FROM
  • WHERE
  • ORDER BY
  • LIMIT
  • GROUP BY
  • HAVING
  • UNION / UNION ALL
  • INTERSECT / EXCEPT
Joins
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
Functions
  • Functions
  • CASE
  • ROUND / FLOOR / CEIL
  • COUNT / SUM / AVG
  • DATE_TRUNC
  • TRIM / LOWER / UPPER
Window Functions
  • Window Functions
  • ROW_NUMBER
  • RANK
  • DENSE_RANK
  • LAG / LEAD
  • NTILE
  • FIRST_VALUE / LAST_VALUE
Subqueries & CTEs
  • Subqueries & CTEs
  • Subquery
  • EXISTS
  • WITH (CTE)
  • Correlated Subquery
  • Recursive CTE
Data Types & NULLs
  • Data Types & NULLs
  • CAST
  • IS NULL / IS NOT NULL
  • COALESCE
  • IFNULL / NULLIF
Patterns
  • Common Patterns
  • Top N Per Group
  • Dedupe Latest Row
  • PIVOT
  • UNPIVOT
Dialect Notes
  • Dialect Notes
  • QUALIFY
Schema (DDL)
  • Schema (DDL)
  • CREATE TABLE
  • ALTER TABLE ADD COLUMN
  • ALTER TABLE RENAME
  • DROP TABLE
  • PRIMARY KEY
  • FOREIGN KEY
  • CREATE INDEX
  • CREATE VIEW
Data Changes (DML)
  • Data Changes (DML)
  • INSERT
  • UPDATE
  • DELETE
  • TRUNCATE
  • MERGE / UPSERT
Transactions
  • Transactions
  • BEGIN / COMMIT / ROLLBACK
Compare
  • Compare
  • INNER JOIN vs LEFT JOIN
  • WHERE vs HAVING
  • UNION vs UNION ALL
  • DELETE vs TRUNCATE vs DROP
  • CTE vs subquery vs temp table
  • COUNT(*) vs COUNT(1) vs COUNT(column)
  • VARCHAR vs TEXT
  • DECIMAL vs NUMERIC vs FLOAT
  • TIMESTAMP vs DATETIME vs TIMESTAMPTZ
  • CHAR vs VARCHAR
  • INT vs BIGINT vs SMALLINT
Recipes
  • Recipes
  • Extract year, month, or day from a date
  • Difference between two dates
  • Convert string to date (and date to string)
  • First day and last day of month
  • Current date, time, and timestamp
  • Extract a substring from a string
  • Split a string by a delimiter
  • Regex in SQL — match, replace, extract
  • Concatenate strings
  • Concatenate rows into a single string
  • Find duplicate rows
  • Delete duplicate rows
  • Rows to columns (pivot)
  • Columns to rows (unpivot)
  • Round a number to N decimal places
  • Calculate a percentage
  • Modulo and remainder
  • Running total (cumulative sum)
  • Previous row value (LAG / LEAD)
  • First or last row per group
  • Rank rows within a group
  • Percentile and median
  • Replace NULL with 0 (or any default)
  • Ignore NULLs in aggregates and window functions
  • NULL comparison: why = NULL never works
  • Convert a string to a number
  • Convert a number to a string
  • ORDER BY with NULLs first or last
  • ORDER BY random: pick random rows
  • Custom sort order with CASE
  • Generate a sequence of numbers
  • Generate dates between two dates (date spine)
  • CASE with multiple conditions
JSON
  • JSON in SQL
  • Extract a field from JSON
  • Postgres jsonb operators
  • Unnest a JSON array into rows
  • Build JSON from rows
  • Update a field inside JSON
Introspection
  • Introspection
  • List all tables in a database
  • Describe a table — show columns, types, and constraints
  • List databases and schemas
  • Find foreign keys for a table
  • INFORMATION_SCHEMA recipes
Home / Reference / Patterns

Patterns

Learn SQL patterns with practical examples and guided drills on dbSyntax.

Start with Common Patterns

Common Patterns

Reusable query shapes for frequent analytics problems. Each page shows the canonical form, the pitfalls (non-deterministic ranking, tie-brea...

Top N Per Group

Pick the best N rows inside each partition. The canonical shape: rank each row within its partition with ROW_NUMBER (or RANK / DENSE_RANK de...

Dedupe Latest Row

Keep one row per key, typically the most recent. A specific case of top-N-per-group with N = 1. Used constantly against event streams, audit...

PIVOT

Turn the distinct values of a column into separate output columns. DuckDB (and SQL Server, Snowflake) offers a built-in PIVOT statement. Els...

UNPIVOT

Turn multiple columns into name/value pairs: one row per (key, column_name, column_value). The inverse of PIVOT, it narrows wide tables into...

dbSyntax Write and Run SQL in Your Browser
CoursePracticeReferenceModelingInterview PrepPrivacyTerms
© 2026 dbSyntax.