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 / Subqueries & CTEs

Subqueries & CTEs

Learn SQL subqueries & ctes with practical examples and guided drills on dbSyntax.

Start with Subqueries & CTEs

Subqueries & CTEs

Break complex logic into readable pieces. A subquery is a query inside another query (in FROM, WHERE, or SELECT). A CTE (WITH ...) names a q...

Subquery

A query nested inside another query. Three common positions: in the FROM clause (derived table), in the SELECT list (scalar subquery, must r...

EXISTS

Predicate that's true if the subquery returns at least one row. The subquery's SELECT list doesn't matter: SELECT 1, SELECT *, SELECT NULL a...

WITH (CTE)

Named query step declared up-front with WITH. Turns a nested subquery into a reusable, top-down read: each CTE is a labeled building block, ...

Correlated Subquery

Subquery that references a column from the outer query, so it's re-evaluated (logically) per outer row. Powerful for per-row comparisons aga...

Recursive CTE

CTE that references itself: iteration inside a single SQL statement. Structure: an anchor SELECT (the seed rows), UNION ALL, and a recursive...

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