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 / JSON

JSON

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

Start with JSON in SQL

JSON in SQL

Read, write, query, and aggregate JSON data inside SQL. Used everywhere data lands as semi-structured payloads: event logs, API responses, u...

Extract a field from JSON

Short answer: the syntax differs more across engines than any other JSON operation. Postgres and DuckDB use -> (returns JSON) and ->> (retur...

Postgres jsonb operators

Postgres ships an entire algebra of operators for the jsonb type — extraction, containment, key existence, path traversal, and merge. Knowin...

Unnest a JSON array into rows

Short answer: explode a JSON-array column into one row per element. Postgres uses jsonb_array_elements (or json_array_elements); BigQuery us...

Build JSON from rows

Short answer: JSON_AGG(col) aggregates rows into a JSON array; JSON_OBJECT('key', val, ...) builds a single object from columns; JSON_OBJECT...

Update a field inside JSON

Short answer: modify a nested value without rewriting the whole document. Postgres uses jsonb_set(col, path, value). MySQL uses JSON_SET. Sn...

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