Code of the Day
Dialect appendices

SQL Server (T-SQL)

Microsoft SQL Server's dialect — TOP, IDENTITY, bracket quoting, and friends.

SQLDialect appendices9 min read
Recommended first
By the end of this lesson you will be able to:
  • Limit rows with TOP and OFFSET/FETCH
  • Use IDENTITY and bracket quoting
  • Swap standard functions for their T-SQL forms

Microsoft SQL Server speaks T-SQL. The core querying you've learned all works; the differences are mostly in syntax for limiting rows, handling, identity columns, quoting, and a handful of functions. (Not runnable here — the engine is SQLite.)

Limiting rows: TOP (and OFFSET/FETCH)

SQL Server has no LIMIT. Use TOP for "the first N":

SELECT TOP 5 name, price FROM products ORDER BY price DESC;

For paging, use the SQL-standard OFFSET … FETCH (which SQL Server supports and LIMIT-using engines also accept):

SELECT name FROM products ORDER BY name
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;

Identity columns

Auto-increment is IDENTITY(seed, increment):

CREATE TABLE customers (
  id INT IDENTITY(1,1) PRIMARY KEY,
  name NVARCHAR(100) NOT NULL
);

Note NVARCHAR for Unicode text — T-SQL distinguishes VARCHAR (ASCII) from NVARCHAR.

Quoting and functions

  • Identifiers are quoted with square brackets: [Order Date] (double quotes also work with a setting). Strings still use single quotes.
  • ISNULL(x, y) replaces a NULL with y — the standard, portable form is COALESCE(x, y), which T-SQL also supports.
  • Current time is GETDATE() (vs CURRENT_TIMESTAMP).
  • String concatenation uses + (or CONCAT(...)), not ||.

Cheat-sheet

Core / standardSQL Server (T-SQL)
LIMIT nSELECT TOP n …
pagingOFFSET … FETCH NEXT …
auto idIDENTITY(1,1)
COALESCE(x,y)ISNULL(x,y) (or COALESCE)
quote identifier[My Column]
concat `a

Where to go next

That completes the dialect appendices. The portable core you learned runs almost unchanged across all three — these tables are the small adjustments per engine.

Finished reading? Mark it complete to track your progress.

On this page