SQL examples
Copy-ready SQL examples for common query patterns, joins, aggregation, window functions, data quality checks and reporting workflows. Each page includes syntax, five practical examples, mistakes to avoid, FAQs and useful resources.
💡 Ideas for You
Learning resources for SQL queries, databases and reporting workflows.
- Step-by-step books
Start with beginner-friendly step-by-step books and learning resources from Create & Learn.
- SQL guide
Helpful references for SELECT, JOIN, GROUP BY, CTEs and window functions.
- Query practice books
Practice reporting queries, data checks and database examples.
- Database reporting
Resources for analytics, data warehouses and SQL reporting patterns.
Some links in this section may be affiliate links. Choose only what is useful for your own work.
Browse SQL examples
Use the filters to find basic queries, joins, aggregation, logic, CTEs, window functions and practical reporting workflows.
SELECT Examples
Select columns and build simple result sets.
BasicsWHERE Examples
Filter rows with conditions.
BasicsORDER BY Examples
Sort query results by one or more columns.
BasicsLIMIT and TOP Examples
Return only a small number of rows for previews or reports.
BasicsDISTINCT Examples
Return unique values and remove duplicates from a result set.
BasicsBETWEEN Examples
Filter values inside a range such as dates, amounts or scores.
BasicsIN Examples
Filter rows where a value matches one of several options.
BasicsLIKE Examples
Search text using wildcard patterns.
BasicsIS NULL Examples
Find missing, blank or unassigned values.
BasicsAliases Examples
Rename columns or tables in a query result.
AggregationCOUNT Examples
Count rows, customers, orders or records.
AggregationCOUNT DISTINCT Examples
Count unique customers, products or IDs.
AggregationSUM Examples
Add amounts such as sales, units or costs.
AggregationAVG Examples
Calculate average amounts, scores or durations.
AggregationMIN and MAX Examples
Find earliest, latest, smallest or largest values.
AggregationGROUP BY Examples
Summarize data by category, month, region or customer.
AggregationHAVING Examples
Filter grouped summary results.
AggregationGROUP BY Date Examples
Summarize records by day, month or year.
AggregationROLLUP Examples
Add subtotal and grand total rows to grouped results.
AggregationPercent of Total Examples
Calculate a row or group as a share of the total.
JoinsJOIN Examples
Combine rows from related tables with matching keys.
JoinsINNER JOIN Examples
Return rows that match in both tables.
JoinsLEFT JOIN Examples
Keep all rows from the left table and matched rows from the right table.
JoinsRIGHT JOIN Examples
Keep all rows from the right table and matched rows from the left table.
JoinsFULL OUTER JOIN Examples
Return matched and unmatched rows from both tables.
JoinsCROSS JOIN Examples
Create every combination of rows from two tables.
JoinsSELF JOIN Examples
Join a table to itself to compare related rows.
JoinsMultiple Joins Examples
Join several tables in one report query.
JoinsAnti Join Examples
Find rows in one table that do not have a match in another table.
JoinsSemi Join Examples
Find rows that have at least one match in another table.
JoinsDuplicate Rows After Join Examples
Understand and fix unexpected duplicated rows after joining tables.
LogicCASE WHEN Examples
Create conditional labels, buckets and flags.
LogicCOALESCE Examples
Return the first non-null value from a list.
LogicNULLIF Examples
Return NULL when two values are equal.
LogicCAST and CONVERT Examples
Change a value from one data type to another.
LogicCONCAT Examples
Combine text values such as names, codes or labels.
LogicUPPER LOWER TRIM Examples
Clean and standardize text values.
LogicSUBSTRING Examples
Extract part of a text value.
LogicDate Functions Examples
Extract, compare and transform dates.
LogicDate Difference Examples
Calculate days or months between dates.
LogicConditional Aggregation Examples
Use CASE inside SUM or COUNT for business summaries.
Subqueries & CTEsSubquery Examples
Use a query inside another query.
Subqueries & CTEsEXISTS Examples
Check whether related rows exist.
Subqueries & CTEsNOT EXISTS Examples
Find records with no related rows.
Subqueries & CTEsCTE Examples
Use WITH clauses to organize complex queries.
Subqueries & CTEsRecursive CTE Examples
Walk hierarchies such as employees, categories or folder trees.
Subqueries & CTEsDerived Table Examples
Query from a subquery in the FROM clause.
Subqueries & CTEsTemp Table Examples
Store intermediate results during multi-step analysis.
Subqueries & CTEsUNION Examples
Stack compatible result sets.
Subqueries & CTEsUNION ALL Examples
Stack result sets without removing duplicates.
Subqueries & CTEsINTERSECT and EXCEPT Examples
Compare sets of rows between queries.
Window FunctionsOVER Examples
Calculate values across rows without collapsing detail.
Window FunctionsPARTITION BY Examples
Restart a window calculation for each group.
Window FunctionsROW_NUMBER Examples
Number rows inside each group.
Window FunctionsRANK Examples
Rank rows while allowing ties to skip numbers.
Window FunctionsDENSE_RANK Examples
Rank rows while keeping tied ranks compact.
Window FunctionsLAG Examples
Return the previous row value.
Window FunctionsLEAD Examples
Return the next row value.
Window FunctionsRunning Total Examples
Calculate cumulative totals over time.
Window FunctionsMoving Average Examples
Calculate rolling averages for trends.
Window FunctionsNTILE Examples
Split rows into buckets such as quartiles or deciles.
WorkflowsFind Duplicates Examples
Find duplicate emails, IDs or records.
WorkflowsRemove Duplicates Examples
Choose one row from duplicate groups before deleting or exporting.
WorkflowsLatest Record Per Group Examples
Get the newest order, status or event per customer.
WorkflowsFirst Record Per Group Examples
Get the earliest order, signup or event per entity.
WorkflowsMissing Records Examples
Find expected records that are not present.
WorkflowsGaps in Dates Examples
Find missing days, months or sequence numbers.
WorkflowsTop N Per Group Examples
Return top products, customers or orders within each group.
WorkflowsSales by Month Examples
Summarize sales into monthly reporting rows.
WorkflowsMonth over Month Examples
Compare current period to the previous period.
WorkflowsCustomer Retention Examples
Track customers who return after their first purchase.
WorkflowsCohort Analysis Examples
Group users by first purchase or signup month.
WorkflowsFunnel Analysis Examples
Count users through ordered steps in a funnel.
WorkflowsInventory Reorder Examples
Flag products below reorder thresholds.
WorkflowsAging Buckets Examples
Put invoices, tickets or tasks into age ranges.
WorkflowsSafe UPDATE Examples
Preview and update rows with careful filters.
No matching examples found. Try another keyword or category.