0 day streak
Code library

Intermediate SQL exercises

Read these Intermediate SQL snippets line by line — each one comes with a written breakdown of what the code does and why.

SQL · Intermediate

Correlated Subqueries

A correlated subquery references columns from the outer query. It is evaluated once for each row processed by the outer query to perform row-specific logic.

Queries & JoinsPerformance
Open snippet →
SQL · Intermediate

Common Table Expressions (CTEs)

A CTE is a temporary result set that you can reference within another statement. It improves readability by breaking down complex queries into logical blocks.

Queries & JoinsSyntax
Open snippet →
SQL · Intermediate

Window Functions with ROW_NUMBER

Window functions perform calculations across a set of table rows related to the current row. ROW_NUMBER assigns a unique sequential integer to rows within a specific partition.

FunctionsQueries & Joins
Open snippet →
SQL · Intermediate

Atomic Transactions with COMMIT

Transactions ensure multiple operations are treated as a single unit. COMMIT saves all pending changes permanently to the database, ensuring data consistency.

TransactionsBest Practices
Open snippet →
SQL · Intermediate

Handling Nulls with COALESCE

COALESCE takes a list of arguments and returns the first non-null value. It is essential for providing fallback values when dealing with optional columns in a database.

FunctionsBest Practices
Open snippet →
SQL · Intermediate

Set Intersection with INTERSECT

The INTERSECT operator returns only the distinct rows that are present in both the first and second query result sets. It is useful for finding common records across different datasets without usin…

Queries & JoinsSyntax
Open snippet →
SQL · Intermediate

Enforcing Uniqueness with Constraints

A UNIQUE constraint ensures that all values in a column are different across all rows. Unlike a PRIMARY KEY, a table can have multiple UNIQUE constraints, and they allow for data integrity and auto…

Indexes & ConstraintsPerformance
Open snippet →
SQL · Intermediate

Conditional Logic with CASE

The CASE expression allows you to add conditional logic to your SQL queries. It evaluates conditions and returns a value when the first condition is met (like an if-then-else statement).

Queries & JoinsSyntax
Open snippet →
SQL · Intermediate

Handling Missing Data Safely with COALESCE

The standard COALESCE scalar function evaluates arguments in sequence and returns the first non-NULL value. It prevents NULL values from propagating through arithmetic operations or output reports,…

Data TypesFunctionsQueries & Joins
Open snippet →
SQL · Intermediate

Handling Null Values Safely with COALESCE

The ANSI SQL standard COALESCE function evaluates arguments sequentially from left to right and returns the first non-NULL value. It prevents unexpected NULL results in calculations and reporting.

FunctionsData Types
Open snippet →