Free Online SQL Snippet Library
Ready to browse the library?
300+ lines of battle-tested SQL code templates. Recursive queries, window functions, pivot tables, and more.
Open SQL Snippets LibraryThe free online SQL snippet library at SQLFormat.io gives you instant access to a curated collection of SQL code templates and recipes. Every developer has written the same recursive CTE, the same ROW_NUMBER dedup pattern, the same pagination query — dozens of times. This library eliminates that repetition. Browse by category, find your pattern, copy, paste, move on.
Why Use a SQL Snippet Library?
SQL is declarative, which means there are often half a dozen ways to solve the same problem. The trick is knowing which one works in your dialect and handles your edge cases. The library doesn't just give you a solution — it gives you the right one, tested across PostgreSQL, MySQL, SQL Server, Oracle, and SQLite where applicable.
- Stop Googling the same patterns — Recursive CTEs, window function syntax, and date truncation are the most-Googled SQL patterns. They're all here.
- Dialect-aware examples — STRING_AGG for PostgreSQL, GROUP_CONCAT for MySQL, LISTAGG for Oracle. Each snippet knows its database.
- One-click copy — No selecting, no scrolling, no missing the trailing semicolon. Just click Copy and paste.
- Zero signup, zero cost — No account, no email, no limits. The snippets run in your browser, just like our other tools.
Category Overview
Recursive CTEs
Employee hierarchies, date range generation, category trees. Anchor + recursion term patterns.
Window Functions
ROW_NUMBER dedup, running totals, LAG/LEAD comparisons, NTILE bucketing, RANK/DENSE_RANK top N.
Pivot & Unpivot
CASE-based cross-tabs, native PIVOT, UNPIVOT, CROSS JOIN LATERAL column-to-row transforms.
Pagination
OFFSET/FETCH, keyset seek method, MySQL LIMIT/OFFSET. Plus performance comparisons.
String Aggregation
STRING_AGG, GROUP_CONCAT, LISTAGG — all four major dialects side by side.
Date & Time
Date truncation, age calculation, overlap detection, date series generation.
Performance
Missing indexes, running queries, table sizes, slow query log analysis.
MERGE / UPSERT
INSERT ON CONFLICT, MERGE, INSERT IGNORE, REPLACE — every upsert pattern.
Example: Recursive CTE for Employee Hierarchy
One of the most commonly requested SQL patterns is the hierarchical query. Given an employees table with columns id, name, and manager_id, you can traverse the management chain from any employee all the way up to the CEO:
WITH RECURSIVE org_tree AS ( SELECT id, name, manager_id, 1 AS level FROM employees WHERE id = 42 UNION ALL SELECT e.id, e.name, e.manager_id, ot.level + 1 FROM employees e JOIN org_tree ot ON e.id = ot.manager_id ) SELECT * FROM org_tree ORDER BY level;
This pattern works in PostgreSQL, SQL Server, and SQLite (MySQL 8+ supports it too). The anchor clause selects the starting employee; the recursive clause climbs the chain. The full library includes similar recipes for date range generation, category path building, and more — all available at the SQL Snippets Library.
Example: Keyset Pagination for Large Datasets
When your table has millions of rows, OFFSET pagination gets progressively slower because the database must scan and discard all skipped rows. Keyset pagination (also called the "seek method") uses a WHERE clause instead:
-- First page SELECT * FROM products ORDER BY product_id LIMIT 10; -- Next page (pass the last product_id from previous page) SELECT * FROM products WHERE product_id > 42 ORDER BY product_id LIMIT 10;
The difference is dramatic: OFFSET 100000 skips 100K rows; WHERE id > 42 uses the index directly. The library includes both approaches with dialect-specific syntax.
Related Tools
Browse the full library now
30+ snippets across 10 categories. All free, all client-side, all one-click copy.
Open SQL Snippets Library