sintaxis-basica-sql

Basic T-SQL Syntax

  • 5 min

Like any language, be it Spanish, English, or C#, T-SQL has its own grammar and spelling. Before we start creating tables or running complex queries, we must understand the basic writing rules.

In this article, we will lay the foundations. We will see how to name things, how to write values, how to leave notes in the code, and how to structure our scripts so that SQL Server understands them.

Identifiers

In T-SQL, the names of objects (like tables, columns, views, databases) are called Identifiers.

Regular Identifiers

These are the ones that follow the standard formatting rules.

  • The first character must be a letter (a-z, A-Z), an underscore _, an at sign @, or a number sign #.
  • Subsequent characters can be letters, numbers, @, $, #, or _.
  • They cannot contain spaces or special characters.
  • They cannot be T-SQL reserved words (like SELECT, TABLE, USER).
SELECT Nombre, Apellido FROM Empleados; -- Correct
Copied!

Delimited Identifiers

Sometimes we need to break the rules. Imagine you inherit an old database where someone named a table Detalle de Ventas (with spaces) or Order (which is a reserved word).

To prevent SQL Server from getting confused, we must enclose the identifier in square brackets [] (Microsoft’s own style) or double quotes "" (ANSI style).

SELECT * FROM [Detalle de Ventas]; -- Necessary because of the spaces
SELECT * FROM [Order];             -- Necessary because Order is a reserved word
Copied!

In the SQL Server world, the predominant convention is to use square brackets []. We will use this notation throughout the course.

Literals

When we write fixed values in the code, we call them literals. Depending on the data type, they are written in one way or another:

They are enclosed in single quotes '.

  • Example: 'Hello World'

What if my text has a single quote? To write something like “O’Connor”, we must double the single quote to “escape” it: 'O''Connor'.

If we are going to store special characters or non-Latin alphabets, we must prefix the string with an uppercase N.

  • Example: N'Here we store emojis 🚀 or kanji'

They are written as is, without quotes. We use a period . for decimals.

  • Example: 10, 15.50, -200

They are generally written as text strings in a specific format. The safest and most international format is ISO YYYY-MM-DD or YYYYMMDD.

  • Example: '2023-12-31'

Comments

Code is read more times than it is written. Therefore, commenting your scripts is a sign of professionalism. T-SQL supports two styles:

-- This is a single-line comment.
-- Everything to the right of the dashes is ignored.

/* This is a comment block
   that can span
   multiple lines.
*/

SELECT * FROM Productos; -- We can also comment at the end of a statement
Copied!

Batches and the GO Command

This is a concept that confuses many at first. In SSMS, you will often see the word GO between statements.

GO is not a T-SQL command. It is an instruction for the client tool (SSMS) that means: “Send everything you have read so far to the server, execute it, and then continue reading.”

Blocks of code separated by GO are called Batches.

Why is it important?

Variables defined in one batch cease to exist in the next one. The scope of a variable is local to the batch.

DECLARE @MiNumero INT = 10;
PRINT @MiNumero;

GO -- The batch ends here. The variable @MiNumero "dies".

-- This will error because @MiNumero no longer exists
PRINT @MiNumero; 

Copied!

The Semicolon (;)

In many languages (like C# or Java), the semicolon is mandatory to end a statement. In T-SQL, historically, it has been optional.

You can write:

SELECT * FROM Clientes
SELECT * FROM Pedidos
Copied!

And it will work perfectly.

However, Microsoft has announced that in future versions it will be mandatory. Furthermore, certain modern statements (like Common Table Expressions or the MERGE statement) require that the previous statement ends with a semicolon.

Get used to always putting a semicolon ; at the end of each statement. It is a good practice that will save you future problems.

Uppercase and Lowercase (Collation)

Is SELECT the same as select? Is 'Hola' equal to 'hOlA'?

T-SQL is case-insensitive for language keywords. Writing SELECT, Select, or select is exactly the same for the engine.

By convention, we usually write keywords in UPPERCASE to make reading easier.

This depends on the Collation configured on the server or database.

  • CI (Case Insensitive): 'A' = 'a'. This is the default setting in most SQL Server installations.
  • CS (Case Sensitive): 'A' != 'a'. In this mode, you must be extremely precise.