A comprehensive guide to SQL query types with examples and variants, applicable across all domains.
← Back to Challenges*bleep* Zero points. The clock is ticking, trainee.
Select specific columns from a dataset.
Select specific columns from a table.
Select all columns using *.
Select unique values.
Filter rows based on conditions.
Filter rows with a single condition.
Combine conditions with AND, OR, NOT.
Sort the result set.
Sort by a column in ascending or descending order.
Sort by multiple columns.
Aggregate rows into groups.
Group rows and apply an aggregate function.
Filter groups based on aggregate conditions.
Apply conditions to grouped results.
Restrict the number of rows returned.
Return a fixed number of rows.
Skip rows before returning a fixed number.
Combine results of multiple SELECT statements.
Combine unique rows from multiple queries.
Combine all rows, including duplicates.
Pattern matching for strings.
Match strings using wildcards.
Filter values within a range.
Include values between two bounds.
Filter rows matching a list of values.
Check if a column value is in a list.
Check for NULL values.
Filter rows where a column is NULL.
Sum numeric values in each group.
Calculate the total of a numeric column.
Count rows or non-null values.
Count all rows.
Average numeric values.
Calculate the average of a numeric column.
Find minimum or maximum values.
Retrieve the smallest or largest value.
Combine two tables row-wise based on a related column.
Returns rows where there is a match in both tables.
Returns all rows from the left table, with matches or NULL.
Compute metrics across a moving frame.
Assign a unique number to each row within a partition.
Common Table Expressions (WITH) define temporary named result sets that make complex queries readable and modular. Think of them as creating a temporary view that only exists for one query.
Use WITH to create a named subquery that you can reference like a table.
Use a CTE to pre-aggregate data, then filter or rank the results in the outer query.
Chain multiple CTEs by separating them with commas. Each CTE can reference the ones defined before it.
Use a CTE to prepare data from one table, then JOIN it with another table in the outer query.
Nest SELECTs inside other queries.
Use a subquery in the WHERE clause.
Apply conditional logic in SELECT.
Transform values based on conditions.
Test whether a subquery returns any rows.
Filter rows based on subquery results.
Assign ranks in windowed results.
Assign ranks with gaps for ties.
Assign ranks without gaps for ties.
Access previous or next row in a window.
Retrieve the previous row’s value.
Retrieve the next row’s value.
Compute distribution quantiles (DuckDB extension).
Calculate a specific quantile of a column.
Map points into spatial grids (DuckDB spatial extension).
Join tables based on spatial relationships.
Rotate groups into columns (DuckDB extension).
Transform rows into columns based on a pivot column.
Handle hierarchical or recursive data.
Traverse a hierarchy.
Pull a path out of JSON text (SQLite JSON1 extension).
Extract a value from a JSON column.
Compare against all or any values in a subquery.
Ensure a condition holds for all subquery values.
Ensure a condition holds for any subquery value.
Return the first non-NULL value from a list of expressions. Essential for handling missing data gracefully.
Replace NULL with a default value.
Try multiple columns in order, returning the first non-NULL.
Use COALESCE to safely handle NULL in math expressions (NULL in arithmetic makes the whole result NULL).
Return NULL if two expressions are equal. Commonly used to prevent division by zero.
Returns NULL when both arguments are the same, otherwise returns the first argument.
Round a numeric value to a specified number of decimal places.
Round to N decimal places.
Round to the nearest whole number by using 0 decimal places.
Convert a value from one data type to another.
Convert between types like TEXT, INTEGER, REAL, DATE.
Cast integers to REAL/FLOAT before dividing to get decimal results instead of integer division.
The OVER clause turns an aggregate or ranking function into a window function. PARTITION BY divides rows into groups (like GROUP BY but without collapsing rows). ORDER BY within OVER defines the row order for the window.
Compute an aggregate per group while keeping every row visible (unlike GROUP BY which collapses rows).
Compute a cumulative sum by ordering rows within each partition.
Rank rows within each group independently.
Retrieve the first or last value in a window frame.
Get the first value within a partition.
Get the last value within a partition. Note — you often need a frame clause to get the true last value.
Divide rows into N roughly equal buckets within a partition.
Split rows into N groups and assign a bucket number.
Concatenate string values from multiple rows into a single string, separated by a delimiter.
Combine values into a comma-separated list.
Functions for manipulating text values.
Replace occurrences of a substring.
Extract a portion of a string by position and length.
Get the number of characters in a string.
Clean and normalize text.
Functions for extracting and formatting dates and timestamps.
Format a date/timestamp into a string (SQLite/DuckDB).
Pull a specific part (year, month, day) from a date.
Simple conditional expressions for handling NULLs and inline if-else logic.
Return the first argument if it is not NULL, otherwise return the second. Shorthand for COALESCE with two arguments.
Inline if-else — returns one value if a condition is true, another if false.
Return the absolute (non-negative) value of a number.
Remove the sign from a number.
Set operations to find differences or commonalities between two result sets.
Return rows from the first query that are NOT in the second query.
Return only rows that appear in BOTH queries.