100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

T-SQL Syntax Basics

Core T-SQL syntax rules — statements, batches, comments, identifiers, and control-of-flow — you need before writing real queries.

FoundationsBeginner9 min readJul 10, 2026
Analogies

T-SQL Syntax Basics

T-SQL statements are the individual instructions you send to the engine — a CREATE TABLE, an INSERT, a SELECT. Each statement is conventionally terminated with a semicolon, and while SQL Server has historically tolerated omitting it in many cases, newer statements (like THROW) require it and Microsoft recommends always including one. A batch is a group of one or more statements sent to the server together for parsing and execution as a unit; GO is the signal client tools like SSMS and sqlcmd use to mark the end of a batch, though GO itself is a client-side convention, not a T-SQL keyword.

🏏

Cricket analogy: A T-SQL statement is like a single delivery bowled in an over: each ending in a semicolon much like each ball being followed by the umpire signaling completion, and a batch (ended by GO) is like a full over sent down before the field resets.

Identifiers, Comments, and Data Literals

Object names (tables, columns) are called identifiers; if a name contains spaces, starts with a digit, or collides with a reserved keyword, you must quote it using square brackets, like [Order Date] or [Group]. Comments come in two styles: -- starts a single-line comment, while /* ... */ wraps a multi-line block. String literals use single quotes, like 'hello', and prefixing a string with N, as in N'café', tells SQL Server to treat it as Unicode (nvarchar) text rather than the default non-Unicode (varchar) interpretation — important when your data includes characters outside the basic Latin alphabet.

🏏

Cricket analogy: Bracketed identifiers like [Player Name] are like using a full name in brackets on a scorecard when a player's name contains a space, such as [Ravindra Jadeja], to avoid the scorer misreading it as two separate columns.

Variables and Control-of-Flow

Local variables are declared with DECLARE, prefixed with @, and given a data type: DECLARE @Count INT. You assign values with SET (one variable at a time) or SELECT (which can assign several at once, or pull a value from a query). Control-of-flow is limited but functional: IF ... ELSE branches based on a condition, and WHILE repeats a block while a condition remains true. Because IF and WHILE each control only the single statement that follows, BEGIN ... END is used to group multiple statements into one logical block when needed.

🏏

Cricket analogy: DECLARE and SET work like a scorer initializing a counter for 'runs needed' before the over starts, then updating it ball by ball: while the required runs are greater than zero, the scoreboard keeps recalculating, just as a WHILE loop keeps executing while its condition holds.

sql
DECLARE @RunsNeeded INT = 30;
DECLARE @BallsLeft  INT = 18;

WHILE @BallsLeft > 0 AND @RunsNeeded > 0
BEGIN
    SET @RunsNeeded = @RunsNeeded - 4;  -- assume a boundary each ball, for demo purposes
    SET @BallsLeft = @BallsLeft - 1;
END

IF @RunsNeeded <= 0
    PRINT 'Target chased down!';
ELSE
    PRINT 'Runs still needed: ' + CAST(@RunsNeeded AS VARCHAR(10));
GO

Batches and the GO Separator

GO is not a T-SQL statement — it's a batch separator understood by client tools like SSMS and sqlcmd, telling the tool to send everything accumulated so far to the server as one batch and start collecting a fresh one. This matters because certain statements (like CREATE PROCEDURE) must be the first statement in a batch, and because local variables declared in one batch cease to exist in the next — each GO effectively resets local variable scope, even though it doesn't affect server-side objects like tables you've already created.

🏏

Cricket analogy: GO acts like the umpire calling 'over': it's not part of any single delivery (T-SQL statement) but a boundary the scorer (SSMS/sqlcmd) recognizes to reset the bowling figures for the next over (batch).

Unlike most programming languages, T-SQL keywords are case-insensitive by default (SELECT, select, and Select all work), but object names can be case-sensitive if the database uses a case-sensitive collation.

  • Every T-SQL statement should end with a semicolon; this is becoming mandatory in newer versions for statements like THROW.
  • GO is a batch separator recognized by client tools (SSMS, sqlcmd), not a T-SQL keyword itself.
  • Use square brackets [ ] to quote identifiers containing spaces or reserved words.
  • String literals use single quotes; prefix with N for Unicode (nvarchar) strings like N'café'.
  • DECLARE defines local variables; SET or SELECT assigns values to them.
  • BEGIN...END groups statements into a block for IF, WHILE, and other control-of-flow constructs.
  • T-SQL keywords are case-insensitive by default, though object name case-sensitivity depends on collation.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SQLServerTSQLStudyNotes#TSQLSyntaxBasics#SQL#Syntax#Identifiers#Comments#StudyNotes#SkillVeris#ExamPrep