SQL Server guide

Turn questions into SQL Server SQL.

T-SQL has its own row limits, date functions and schema conventions. Define the SQL Server version and the result you need before asking a model to translate the question.

Try TTSQL free

Before connecting SQL Server

Supply host, port, database, username and password for a SQL Server instance reachable by TTSQL. The adapter uses the Tds driver; confirm database authentication and the instance's network listener with your administrator.

Choose the schema containing your reporting tables. The examples use dbo.orders. A table in sales.orders is a different object and should be named explicitly in project context.

Give the prompt the right dialect and context

SQL Server commonly uses square brackets to quote identifiers, such as [order]. Use single quotes for strings and schema-qualified names such as dbo.orders. PostgreSQL LIMIT and ::date are not T-SQL syntax.

Use datetime2 for the teaching ordered_at column. In SQL Server, timestamp is a rowversion binary value, not a date and time type. The examples use features available in SQL Server 2012 and later.

Schema used in these examples

customers(id INTEGER PRIMARY KEY, name TEXT, country TEXT); orders(id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers(id), ordered_at TIMESTAMP, status TEXT, total DECIMAL(12,2)). Each order has one customer. total is the order amount in a single reporting currency; paid means recognized revenue in this teaching dataset. Timestamps use UTC. These are example tables, not tables TTSQL creates.

Translate TEXT and TIMESTAMP to equivalent column types for your engine. Use your own database, schema and table qualifiers. Each example states its reporting grain and sorting rule.

Review these SQL Server details

OFFSET/FETCH pagination requires ORDER BY; a stable unique order avoids overlapping or missing rows caused by ties, although concurrent data changes can still affect offset paging.

Keep dates typed and use unambiguous date construction or parameter values. Do not assume a language-dependent string such as '09/05/2026' means the same date on every SQL Server session.

Generate, review, then choose whether to run

Select the matching database type and confirm your project's schema context before generating. Review the returned SQL against the schema and the metric you intended. A successful connection test does not prove that every table or operation is authorized.

TTSQL's POST /api/v1 endpoint generates SQL and returns executed: false. Query execution is a separate manual Run action in the workspace, subject to the configured connection and policies. The examples below are educational SELECT statements, not live results from your database.

Official SQL Server references

Check your engine version and account configuration when adapting a function or connection setting.

Worked SQL examples

Read the sample schema and assumptions alongside each query. Adapt names, dates, and definitions to your own database before running SQL.

01 / SQL Server 2012+

Choose the five largest paid orders

Show the five highest-value paid orders; break equal totals by ascending order ID.

SELECT TOP (5) id, total
FROM dbo.orders
WHERE status = 'paid'
ORDER BY total DESC, id;

TOP belongs next to SELECT in T-SQL. ORDER BY supplies the meaning of 'highest' and the ID supplies a stable cutoff. TOP WITH TIES would have different semantics and could return more than five rows.

02 / SQL Server 2012+

Create a month-start date

Sum paid revenue by calendar month in stored timestamps and sort chronologically.

SELECT DATEFROMPARTS(YEAR(ordered_at), MONTH(ordered_at), 1) AS month,
       SUM(total) AS revenue
FROM dbo.orders
WHERE status = 'paid'
GROUP BY DATEFROMPARTS(YEAR(ordered_at), MONTH(ordered_at), 1)
ORDER BY month;

DATEFROMPARTS returns a typed first-of-month date. Grouping by both year and month avoids merging years. This pattern also works on versions that predate SQL Server 2022's DATETRUNC function.

03 / SQL Server 2012+

Use a CTE to filter a window rank

Return each customer's latest paid order, breaking timestamp ties by the greatest ID. Sort by customer ID.

;WITH ranked AS (
  SELECT customer_id, id, total,
         ROW_NUMBER() OVER (
           PARTITION BY customer_id ORDER BY ordered_at DESC, id DESC
         ) AS rn
  FROM dbo.orders
  WHERE status = 'paid'
)
SELECT customer_id, id, total
FROM ranked
WHERE rn = 1
ORDER BY customer_id;

The leading semicolon terminates any preceding statement before the CTE. ROW_NUMBER isolates exactly one row per customer; RANK could preserve multiple tied rows if the ordering were not unique. The outer query filters after ranking.

Answers before you connect a database.

Does selecting SQL Server guarantee a working database connection?

No. SQL generation uses the project dialect and schema, while connection and execution also depend on deployment prerequisites, credentials, network access and database permissions. Complete the prerequisites described in this guide.

Does the TTSQL API execute SQL Server queries?

No. POST /api/v1 generates SQL and returns executed: false. Review the query; a separate manual Run action is available in the workspace when the connection and policies allow it.

Bring your next question to TTSQL.

Start with one PostgreSQL project and 20 free requests a day. Explore your data, build a report, or integrate the API.