Azure-Sql-Db

T-SQL 101: 105 Using modern join syntax

There are two ways you can write common join syntax. There’s an older way to write the query and a new way to write the query.

In the image above, you can see the new way written first, and then the old way written below. The newer syntax was introduced as part of ANSI SQL, back in 1992. It has been recommended for over 30 years now.

Comparison

When you look at the older syntax, you’ll notice that I’m doing two things in the WHERE clause. I’m both determining how the tables are joined, and I’m also filtering the rows that I want.

2025-02-01

T-SQL 101: 104 Using an INNER JOIN

The most common form of join between two tables is what’s called an inner join.

In the example shown above, I’ve selected some columns from different tables. I’ve got description and product ID from the product table and I’ve got the product group name from the product group. What I wanted was a list of products but I want to know what group they’re part of.  So to do that I had to use the products table, but I also had to join to the product groups table so I could get the name of the product group.

2025-01-31

T-SQL 101: 103 What is a Foreign Key ?

A foreign key is a situation where we store the primary key of one table in another table. It’s very useful. For example, we might need to know which sales transactions were for which customers.

Again, keep in mind that like primary keys, there could be more than one column in a key. While a foreign key can reference a unique key, it’s almost always the primary key.

In the example shown above, the column OrderID is the primary key of the Orders table. It’s highlighted in red. The column CinemaID is the primary key of the Cinemas table, but it has been stored in the Orders table. It’s highlighted in green. We refer to that as a foreign key. Clearly in a system that handles orders for cinemas, we’d need to know which cinema each order is for.

2025-01-30

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

SQL Server Execution Plans for Developers and DBAs - new online on-demand course now available

One of the most popular courses that we used to run in person was a Query Performance Tuning and Advanced T-SQL course. I recently finished converting the Advanced T-SQL course to an online format, but a key (no pun intended) part of the Query Performance Tuning course was the content on reading SQL Server execution plans.

For so long, I’ve wanted to get the execution plan content available online, as reading them is such an important skill. Well, we finished it today ! The new course is:

2024-10-30