⚙️ Settings
WITH recent_orders AS (
SELECT
customer_id,
order_id,
order_date,
total_amount
FROM orders
WHERE order_date >= DATEADD(day, -30, GETDATE())
)
SELECT
customer_id,
COUNT(order_id) AS order_count,
SUM(total_amount) AS total_spent
FROM recent_orders
GROUP BY customer_id
ORDER BY total_spent DESC;
WITH RECURSIVE employee_hierarchy AS (
-- Anchor: top-level rows (no manager)
SELECT
employee_id,
manager_id,
employee_name,
1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
-- Recursive: join children to their parent's result
SELECT
e.employee_id,
e.manager_id,
e.employee_name,
eh.level + 1
FROM employees e
INNER JOIN employee_hierarchy eh
ON e.manager_id = eh.employee_id
)
SELECT *
FROM employee_hierarchy
ORDER BY level, employee_name;
-- Note: SQL Server / Oracle: drop the RECURSIVE keyword (just WITH employee_hierarchy AS (...))
SELECT
o.order_id,
c.customer_name,
o.order_date,
p.product_name,
oi.quantity
FROM orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
LEFT JOIN order_items oi
ON o.order_id = oi.order_id
LEFT JOIN products p
ON oi.product_id = p.product_id
WHERE o.order_date >= '2026-01-01'
ORDER BY o.order_date DESC;
SELECT
DATE_TRUNC('month', order_date) AS order_month, -- PostgreSQL
-- FORMAT(order_date, 'yyyy-MM') AS order_month, -- SQL Server
-- DATE_FORMAT(order_date, '%Y-%m') AS order_month, -- MySQL
COUNT(*) AS order_count,
SUM(total_amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY order_month;
SELECT
customer_id,
order_id,
order_date,
total_amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS order_rank,
SUM(total_amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders;
CREATE PROCEDURE GetCustomerOrders
@CustomerId INT,
@StartDate DATE = NULL,
@EndDate DATE = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT
order_id,
order_date,
total_amount
FROM orders
WHERE customer_id = @CustomerId
AND (@StartDate IS NULL OR order_date >= @StartDate)
AND (@EndDate IS NULL OR order_date <= @EndDate)
ORDER BY order_date DESC;
END;
-- Call it: EXEC GetCustomerOrders @CustomerId = 101, @StartDate = '2026-01-01';
MERGE INTO customers AS target
USING staging_customers AS source
ON target.customer_id = source.customer_id
WHEN MATCHED THEN
UPDATE SET
target.customer_name = source.customer_name,
target.email = source.email,
target.updated_at = GETDATE()
WHEN NOT MATCHED THEN
INSERT (customer_id, customer_name, email, created_at)
VALUES (source.customer_id, source.customer_name, source.email, GETDATE());
SQL Query Templates — Copy-Paste Boilerplate for CTEs, Joins & Stored Procedures
SQL Query Templates is a library of ready-to-use boilerplate SQL for common patterns — CTEs, joins, date rollups, window functions, stored procedures, and upserts. It's built for developers and analysts who need a SQL CTE example or a SQL window function example to start from, instead of looking up syntax from scratch every time. Browse by category, copy a template, and adapt it to your own table and column names.
⚡ Key Takeaways
- Six categories: CTEs, joins, date rollups, window functions, stored procedures, upserts.
- Every template is a complete, realistic structure — not a bare minimal example.
- Written in standard SQL with inline notes on engine-specific variations.
- A faster starting point than looking up syntax from scratch every time.
What, Who, When & Why
| What it's for | A library of ready-to-copy SQL boilerplate for CTEs, joins, date rollups, window functions, stored procedures, and upserts. |
|---|---|
| Who it's for | Developers and analysts who don't want to look up correct syntax from scratch every time. |
| When to use it | When starting a query pattern you don't write often enough to remember the exact syntax for. |
| Why it's needed | Looking up correct SQL syntax repeatedly wastes time, and small syntax mistakes in patterns like recursive CTEs are easy to make from memory. |
| Best way to use it | Read each template's short description before copying to confirm it matches your exact use case, then rename placeholder tables and columns immediately. |
How to Use SQL Query Templates
- Browse the templates, organized by category: CTEs, Joins, GROUP BY / Dates, Window Functions, Stored Procedures, and Upsert / Merge.
- Read the short description under each template to confirm it fits your use case.
- Click Copy on the card to grab that template's SQL.
- Paste it into your editor and swap in your own table and column names.
What's Included
| Category | Templates |
|---|---|
| CTE | Basic CTE, Recursive CTE (hierarchy/org chart) |
| Joins | INNER/LEFT JOIN across multiple tables |
| GROUP BY / Dates | Monthly rollup with date truncation |
| Window Functions | ROW_NUMBER with a running total |
| Stored Procedures | Parameterized procedure with optional filters |
| Upsert / Merge | MERGE statement from a staging table |
Each template is written to be immediately readable and adaptable — rather than a minimal one-line example, every template shows a realistic, complete structure you'd actually use in a real query, including sensible variable and column naming that makes it clear what to swap out for your own schema.
Key Features
- Six categories of common, real-world SQL patterns.
- Each template includes a short description of what it's for.
- One-click copy for each individual template.
- Written in standard, widely-portable SQL syntax with notes on engine-specific variations where relevant.
Practical Use Cases
- Getting started quickly: skip the syntax lookup and start editing a working query instead of writing one from a blank page.
- Learning SQL patterns: study a correct, working example of a CTE, window function, or stored procedure structure.
- Standardizing team practices: use consistent boilerplate patterns across a team's queries.
- Building monthly reports: start from the date-rollup template for recurring reporting queries.
- Interview or exam preparation: review correct syntax patterns for common SQL interview topics like window functions and CTEs.
Tips for Best Results
- Read the short description on each template before copying, to confirm it matches the specific pattern you need rather than a similar-looking one.
- Rename the placeholder table and column names immediately after pasting, so you don't accidentally run a query against tables that don't exist.
- Use the recursive CTE template as a learning reference even if your immediate need is simpler — it demonstrates a pattern that's useful for hierarchical data in many contexts.
Related Terminology
A CTE (Common Table Expression) is a named, temporary result set defined with a WITH clause, useful for breaking a complex query into readable steps. A window function performs a calculation across a set of rows related to the current row (like a running total) without collapsing them into a single result, unlike a standard aggregate. An upsert (via MERGE) inserts new rows and updates matching existing ones in a single statement.
Important Considerations
Templates use standard SQL syntax where possible, but some features (like date truncation functions or recursive CTE syntax) vary between database engines — check the inline notes in each template for engine-specific variations (PostgreSQL, SQL Server, MySQL) before running it. Templates are starting points, not finished queries — always review and test against your actual schema before running in a production environment.
Related Tools
Once you've adapted a template, format it with the SQL Formatter, or get a plain-English explanation of your final query with the SQL Query Explainer. Building a CASE statement instead? Try the CASE Builder.
Frequently Asked Questions
Are these templates written for a specific database engine?
They use standard SQL where possible, with inline comments noting variations for PostgreSQL, SQL Server, and MySQL where syntax differs between engines.
Can I use these templates directly, or do I need to edit them?
You'll need to swap in your own table and column names — the templates provide a correct, working structure to build from rather than being ready to run unmodified.
What's a good starting template for a monthly report?
The GROUP BY / Dates template shows how to roll up data by month using date truncation, a common pattern for recurring reports.
Is there an example for hierarchical data, like an org chart?
Yes, the Recursive CTE template demonstrates how to traverse hierarchical relationships like an employee-to-manager structure.
#FACC15
rgb(250, 204, 21)
rgb(98%, 80%, 8%)
hsl(46, 96%, 53%)
hsv(46, 92%, 98%)
cmyk(0%, 18%, 92%, 2%)
Tools for analysts, developers & QA engineers
33 free, browser-based utilities — text and list tools, SQL helpers, converters, and small productivity apps. Everything runs locally; nothing you type or paste is ever uploaded.
For Analysts
Clean lists, build SQL fragments, and reshape data without opening a spreadsheet.
For Developers
Format SQL, convert data formats, and handle everyday text and encoding tasks.
For QA Engineers
Generate test data, compare text output, and sanitize queries before sharing them.