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 / Window Functions

Window Functions

Learn SQL window functions with practical examples and guided drills on dbSyntax.

Start with Window Functions

Window Functions

Compute rankings, running totals, and moving metrics without collapsing rows. A window function sees a group of rows (the "window") and retu...

ROW_NUMBER

Assign a unique sequential number to each row within a partition, in a defined order. The workhorse window function — powers top-N-per-group...

RANK

Rank rows by an ordered metric. Ties share a rank, and the next rank skips the count of ties (1, 2, 2, 4). Use RANK when "tied for 2nd, nobo...

DENSE_RANK

Rank rows without gaps: ties share a rank, and the next rank is always the next integer (1, 2, 2, 3). Use DENSE_RANK when you care which buc...

LAG / LEAD

Read a value from the previous (LAG) or next (LEAD) row within a partition, in a defined order. The backbone of row-over-row metrics: month-...

NTILE

Split ordered rows into N equal-sized buckets: quartiles (NTILE(4)), deciles (NTILE(10)), percentiles (NTILE(100)). If the row count doesn't...

FIRST_VALUE / LAST_VALUE

Return the first or last value from a window frame. FIRST_VALUE with the default frame does what you'd expect. LAST_VALUE almost never does....

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