SQL Snippets: Where to Save Them and Libraries to Use

· 7 MIN

TL;DR: SQL snippets are saved pieces of SQL you insert with a short trigger word instead of retyping them. SSMS stores them as .snippet XML files, VS Code as JSON .code-snippets files, DBeaver as Templates and DataGrip as Live Templates. If the snippet you keep reusing is really a business question ("revenue by month", "active users this week"), AI for Database lets you ask it in plain English, shows the SQL, and pins the answer to a dashboard that refreshes itself.

SQL snippets (also called SQL templates or live templates) are reusable fragments of SQL, such as a standard join, a date filter or a TRY...CATCH block, that your editor inserts when you type a prefix and press Tab. Every major SQL editor supports them, but each stores them in a different place and format: SSMS uses XML .snippet files registered in Tools > Code Snippets Manager, VS Code uses JSON with ${1:placeholder} tab stops, DBeaver and DataGrip use template settings pages. Below are the exact paths, a sample snippet for each, and the snippet libraries worth importing.

Try AI for Database free at aifordatabase.com (https://app.aifordatabase.com/signup, sample data included).
How a snippet goes from a saved file to finished SQL in the editor
FIG 01 — Same flow in SSMS, VS Code, DBeaver and DataGrip; only the storage format differs.

What AI for Database does for the queries you keep saving

Most snippet collections end up as a pile of report queries: monthly revenue, signups by source, churned accounts. AI for Database handles that job differently for teams without a data team:

It does not manage editor snippets for you. Keep using the editor tools below for DDL, admin scripts and code you write yourself.

How do I add custom snippets in SSMS?

SSMS snippets are XML files with a .snippet extension that follow the Visual Studio Code Snippets Schema with the language set to SQL (Microsoft Learn).

  1. Save the file, for example TopN.snippet.
  2. Open SSMS, go to Tools > Code Snippets Manager.
  3. Choose Add to register a whole folder, or Import to add one file to My Code Snippets.
  4. In a query window press Ctrl+K, Ctrl+X to insert it.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Top N rows</Title>
      <SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal><ID>n</ID><Default>100</Default></Literal>
        <Literal><ID>table</ID><Default>dbo.Orders</Default></Literal>
      </Declarations>
      <Code Language="SQL"><![CDATA[SELECT TOP ($n$) * FROM $table$ ORDER BY 1 DESC;]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

To see where the built-in T-SQL snippets live on your machine, pick SQL in the Language box of Code Snippets Manager; the path appears under Location.

Do VS Code and DBeaver support SQL snippets?

Yes, both do, in different formats.

VS Code (and the mssql extension) uses JSON snippets. Open File > Preferences > Configure Snippets, pick sql or create a global .code-snippets file. A project file in .vscode/ is shared with everyone who opens the repo (VS Code docs). ${1:default} is a tab stop and $0 is where the cursor ends. Tab expansion needs "editor.tabCompletion": "on".

{
  "Rows in the last N days": {
    "prefix": "lastn",
    "scope": "sql",
    "body": [
      "SELECT *",
      "FROM ${1:orders}",
      "WHERE ${2:created_at} >= CURRENT_DATE - INTERVAL '${3:7} days';$0"
    ],
    "description": "Filter a table to the last N days (PostgreSQL)"
  }
}

Azure Data Studio used the same format, but Microsoft retired it on February 28, 2026 and points users to VS Code (Microsoft Learn). Copy your old sql.json snippets into VS Code as they are.

DBeaver calls them SQL Templates. Press Ctrl+Alt+Space to list them, or type the template name and press Tab. Add or edit them under Preferences > SQL Editor > Templates (DBeaver docs).

-- DBeaver template "dup": find duplicate values
SELECT ${column}, COUNT(*) AS n
FROM ${table}
GROUP BY ${column}
HAVING COUNT(*) > 1;

DataGrip uses Live Templates: Ctrl+J inserts, Tab expands, and Settings > Editor > Live Templates manages them. Built-ins include sel, selw, selc, ins and upd (JetBrains docs).

EditorName for snippetsFormatWhere to manageInsert
SSMSCode snippets.snippet XMLTools > Code Snippets ManagerCtrl+K, Ctrl+X
VS CodeSnippets.code-snippets / sql.jsonFile > Preferences > Configure Snippetsprefix + Tab
DBeaverSQL Templatessettings pagePreferences > SQL Editor > Templatesname + Tab, Ctrl+Alt+Space
DataGripLive Templatessettings pageSettings > Editor > Live Templatesabbreviation + Tab, Ctrl+J
Databricks SQLQuery snippets${1:value} in UIView > Query snippetsautocomplete, Ctrl+Space
MetabaseSnippets{{snippet: Name}}SQL editor sidebar{{snippet: autocomplete
The same date filter stored in three editors' formats
FIG 02 — Placeholder syntax is the main thing that changes when you move snippets between tools.

Where can I find ready-made SQL snippet libraries?

Start with these, grouped by kind. All are free unless noted.

Snippet collections

Editor documentation

Reference queries to turn into snippets

Our free pages

What makes a SQL snippet worth saving?

A snippet is worth saving when you type the same structure at least weekly and only the names change. Good candidates:

  1. Boilerplate with fixed syntax: TRY...CATCH, MERGE, CREATE TABLE with your audit columns.
  2. Easy-to-get-wrong filters: half-open date ranges (>= start AND < end), timezone conversion.
  3. Admin lookups: find a column across tables, table sizes, blocking sessions.
  4. Team definitions: "active user" or "net revenue", shared in a project .code-snippets file or a Metabase snippet so everyone uses one definition.

Skip it when the query is a one-off report someone else will ask for again next month: that is a dashboard, not a snippet. For PostgreSQL work, see our PostgreSQL page.

Start here

  1. Pick your editor's row in the table above and create one snippet for the query you typed most this week.
  2. Import three analytics snippets from count/sql-snippets for your database.
  3. Put team definitions in a shared project snippet file.
  4. Move recurring report queries out of snippets and into a scheduled dashboard.

Frequently Asked Questions

What is a SQL snippet?

A SQL snippet is a saved, reusable fragment of SQL that an editor inserts when you type a short prefix, often with placeholders you fill in with Tab.

How do I add custom snippets in SSMS?

Write an XML .snippet file with language SQL, then register it in Tools > Code Snippets Manager with Add (folder) or Import (file).

Do VS Code and DBeaver support SQL snippets?

Yes. VS Code uses JSON snippet files (sql.json or .code-snippets); DBeaver uses SQL Templates under Preferences > SQL Editor > Templates.

Can I move Azure Data Studio snippets to VS Code?

Yes. Azure Data Studio used the VS Code snippet format, so the same JSON works in VS Code, which Microsoft recommends after the February 28, 2026 retirement.

Where can I find ready-made SQL snippet libraries?

count/sql-snippets on GitHub is the broadest free collection, with versions for BigQuery, MSSQL, MySQL, Postgres and Snowflake. > Stop pasting the same report query every week. Try AI for Database free at aifordatabase.com (https://app.aifordatabase.com/signup, sample data included).

Ready to try AI for Database?

Query your database in plain English. No SQL required. Start free today.