SQLMentor // learn sql server

Introduction to SQL Server & T-SQL

T-SQL (Transact-SQL) is Microsoft's procedural extension to ANSI SQL, the native language of Microsoft SQL Server and Azure SQL. Where ANSI SQL is purely declarative, T-SQL adds variables, control flow, error handling, batches, and a rich library of built-in functions — all executed inside the database engine.

A Brief History

T-SQL began life at Sybase in the late 1980s. Microsoft licensed the Sybase code base in 1988 to ship SQL Server on OS/2, then rewrote it in the mid-1990s for Windows NT (SQL Server 6.0/6.5, then the major 7.0 rewrite in 1998). Both Microsoft SQL Server and Sybase ASE still share T-SQL ancestry, though the dialects have diverged considerably over three decades.

Editions and Flavors

Edition Use case
Express Free, capped at 10 GB per database, ideal for small apps and learning
Developer Full Enterprise features for non-production use — free
Standard Mid-tier production workloads
Enterprise Full feature set: partitioning, in-memory OLTP, advanced HA, unlimited cores
Azure SQL Database Fully managed PaaS — single database
Azure SQL Managed Instance Near-100% SQL Server surface area, fully managed
SQL Server on Linux Available since SQL Server 2017

What T-SQL Adds Over ANSI SQL

  • Procedural constructs: IF/ELSE, WHILE, BEGIN ... END blocks
  • Local and table variables (DECLARE @v INT, DECLARE @t TABLE(...))
  • Structured error handling (BEGIN TRY ... BEGIN CATCH, THROW)
  • The batch concept — a group of statements compiled and executed together, separated by GO
  • Enhanced DML: OUTPUT clause, MERGE, TOP n WITH TIES, OFFSET-FETCH
  • Window functions with ROWS/RANGE framing
  • Hundreds of system functions: GETDATE(), ISNULL(), OBJECT_ID(), STRING_AGG()

Batches and the GO Terminator

A batch is a unit of statements sent to the server together for parsing, binding, and optimisation. GO is not a T-SQL keyword — it's a separator recognised by SSMS, sqlcmd, and Azure Data Studio. The client splits the script on GO and sends each batch separately.

-- Two separate batches:
SELECT @@VERSION;
GO

USE HR;
GO

CREATE PROCEDURE dbo.HelloWorld AS
    SELECT 'Hello, T-SQL!' AS greeting;
GO

EXEC dbo.HelloWorld;
GO

GO can also accept a count: GO 100 runs the preceding batch 100 times — handy for load tests.

Comments

-- Single-line comment

/*
   Multi-line block comment.
   Can be nested in T-SQL: /* inner */ outer.
*/

SELECT first_name -- inline comment
FROM   employees;

Identifier Rules

  • Up to 128 characters.
  • Must begin with a letter, _, @, or #. @ denotes a local variable, # a temp table, ## a global temp table.
  • Wrap special names or reserved words in square brackets or double quotes (with QUOTED_IDENTIFIER ON).
CREATE TABLE [Order Details] (
    [Order ID]   INT NOT NULL,
    "Item Name"  NVARCHAR(100)
);

DECLARE @count INT = 0;     -- local variable
SELECT * FROM #temp;        -- session-scoped temp table
SELECT * FROM ##shared;     -- global temp table

Your First T-SQL Script

USE HR;
GO

DECLARE @greeting NVARCHAR(100) = N'Hello, T-SQL!';
DECLARE @ceo_name NVARCHAR(100);

SELECT @ceo_name = first_name + ' ' + last_name
FROM   employees
WHERE  employee_id = 100;

PRINT @greeting;
PRINT 'CEO: ' + ISNULL(@ceo_name, '<not found>');
GO

Summary

  • T-SQL is Microsoft SQL Server's procedural extension to SQL, descended from Sybase.
  • Editions range from free Express/Developer to Enterprise and Azure SQL.
  • Batches are parsed and optimised together; GO is a client-side separator, not T-SQL.
  • The HR schema (employees, departments, jobs, locations, customers, orders) is used throughout this guide.