T-Sql 101

T-SQL 101: 102 What is a Primary Key ?

In the previous post, we saw how CROSS JOIN operations work. Now it’s rare for us to want to have every combination of the rows in each table.

What we usually end up joining on are primary keys and foreign keys. So we should start by defining what primary keys are.

The primary key of a table is a value or combination of values that uniquely identifies a particular row in a table. If we need to refer to a row in a table, and ensure that we’re only talking about one row, it’s how we do that. So how do we identify that one row exactly?

2025-01-29

T-SQL 101: 101 Using CROSS JOIN

The simplest way to join two tables is what’s called a cross-join. So this is where I say from products, cross-join, sales territories.

SELECT *
FROM dbo.Products
CROSS JOIN dbo.SalesTerritories;

Now what that will do though is it will give me every combination of rows from the two tables. So I’ll get one row from the second table for each row in the first table. This is often called a Cartesian product of the two tables and it can quickly end up generating a very large number of rows.

2025-01-28

T-SQL 101: 100 Creating loops in T-SQL with WHILE

Most programming languages have some way of forming loops. Now some of them have a variety. For example, a language like Basic or Excel had a For Next loop, a Do While loop, a While Until loop, etc.

In T-SQL, while you could conceivably construct loops manually by using a GOTO statement, no-one does that and it would be considered a bad idea.

Instead, we form all the loops we need by using a WHILE statement. It can be used to create all the variety of loops mentioned above.

2025-01-27

T-SQL 101: 99 Applying conditional logic with IF

In general, we try to avoid procedural logic when writing T-SQL. However, it is possible.

The most basic procedural statement is the IF statement. It allows us to apply basic conditional logic. Instead of keywords like CASE that allow you to apply logic to determine values, the IF statement allows you to decide which statements are executed.

IF @Value > 10 PRINT 'Large';

The condition can also include other clauses linked with AND and OR:

2025-01-26

T-SQL 101: 98 Using System Variables and Functions in T-SQL

As well as the variables that you declare, SQL Server has a number of built-in system variables and some built-in system functions.

The system variables are the ones with @@ at the start of their name. Here is an example that’s often used:

SELECT @@VERSION;

It is documented here.

On my SQL Server 2022 system, the value returned is this:

Now while that is a reasonable example of a system variable that has been used for a long time, note that it’s a bit messy as there is a lot of information in a single returned value. There are now better ways to get at the system version in a more useful way:

2025-01-25

T-SQL 101: 97 Defining and initializing variables in T-SQL

Variables in T-SQL work very much the way they do in other languages. A variable really is nothing more than a name given to a memory location. In T-SQL, variable names must start with an @ sign. An example would be:

@CustomerName

As well as defining a name, variables have other properties.

The first is that we have a data type. It defines the types of things, like strings or numbers and so on, that can we store in that location.

2025-01-24

T-SQL 101: 96 Choosing from alternatives with IIF in SQL Server T-SQL

In my last T-SQL 101 post, I described the CASE statement. Until SQL Server 2012, that was the only real option that we had for choosing between alternate values. In SQL Server 2012, Microsoft gave us another option with the IIF function.

The IF function is very similar to the IF function in Microsoft Excel. It takes three parameters:

  • A boolean value to check (normally this is an expression)
  • A value that will be returned if the first parameter is true.
  • A value that will be returned if the first parameter is false.

In the main image above, you can see the equivalent CASE statement and the simplified IF function equivalent.

2021-04-26

T-SQL 101: 95 Choosing options with CASE in T-SQL

You often need to choose from many outcomes, based upon a specific value. You might want to say if the value is 3 then choose Red, but if the value is 4 then choose Blue, and if the value is 5, then choose Green, etc. The way that you apply this type of logic in T-SQL is by using the CASE statement.

There are several ways you can write CASE statements. In the main image above, I have said that if the value is greater than or equal to 25, then say “Large”. If the value was between 20 and 24, then “Medium”. Otherwise (for all other values), choose “Small”.

2021-04-19

T-SQL 101: 94 Returning messages to the client by using PRINT and RAISERROR in T-SQL

There are times when you’re writing in any programming language, that you want to output messages back to the client. In T-SQL, the statement provided for that is the PRINT statement.

PRINT is simple enough to use. You give it a  string to return and it prints that string.

It’s different to the SELECT statement, because the SELECT statement returns a rowset (i.e. a set of rows).

In the main image above, you can see a script with both a PRINT and a SELECT. When that script is executed, two things happen. The output of the SELECT statement is returned in the Results tab in SQL Server Management Studio as below:

2021-04-12

T-SQL 101: 93 Restarting row numbering by using PARTITION BY

Imagine that I’ve used ROW_NUMBER to number all the cinemas in my database. I’ll get values from 1 to the maximum number of cinemas. But what if I want to number the cinemas within each city? i.e. Aberdeen has three cinemas, so number them 1, 2, and 3. But when we get to the next city, start the numbering again. We can do this by adding PARTITION BY to the OVER clause.

2021-04-05