T-Sql 101

T-SQL 101: 112 Excluding Data with EXCEPT

We saw how UNION and UNION ALL worked in the last T-SQL 101 post. Sometimes you want to work with two (or more) row sets in other ways.

The EXCEPT clause says that I want all the distinct entries in the first row set unless they also exist in the second row set.

In some database engines, this operator is called MINUS, but EXCEPT is the ANSI SQL standard, and that’s what SQL Server uses.

2025-02-09

T-SQL 101: 111 Using UNION and UNION ALL

There are times when you need to connect together two sets of results into a single result set. The UNION statement is the way we do that.

In the example shown above, I have two SELECT queries. Notice that I could just highlight either one of them, and run them, and I’d see those values. But if I run the whole query, I’ll get back a single set of results that combines data from each of the queries. Note the results:

2025-02-08

T-SQL 101: 110 Joins without equality (non-equi joins)

I’ve made several posts about joins but one option that I haven’t talked about as yet, are what are called non-equi joins.

These are less common, and they often don’t optimize all that well, but it’s worth noting that joins don’t always have to be on equality, or what are called equi-joins.

Non-equi join sounds complex but all we’re saying is that the things that we’re joining on are not equal signs.

2025-02-06

T-SQL 101: 109 Joining a table to itself (self joins)

One thing you might not have considered is that a table can also be joined to itself.

Imagine that I want a list of employees, but I also want their manager’s details, the employee ID and the name for the manager. Now the problem is they’re both in the same table, but they’re different rows in that same table because a manager is also an employee.

The way I did this in the example is to say that I’m reading from Employees and then joining to Employees i.e. the same table.

2025-02-05

T-SQL 101: 108 Joining more than two tables

Of course, you might also need to join more than two tables. The example above shows how to do that.

I started with the Orders table, then joined to the OrderLines table, and finally, I’ve also joined to Products.

It’s another example of why I prefer the modern join syntax. Instead of just listing three tables and a bunch of WHERE clause predicates, for each table involved, I specify how it’s joined.

2025-02-04

T-SQL 101: 107 Using other outer joins with RIGHT OUTER JOIN, FULL OUTER JOIN

After discussing LEFT OUTER JOIN, you might wonder if there is also a RIGHT OUTER JOIN. There is, and it’s the reverse of the left one.

What a RIGHT OUTER JOIN from Products to ProductGroups says is that I want at least one row for every row in ProductGroups. You can see that in the query example above. The product group Hot Food currently has no products in it. The RIGHT OUTER JOIN causes it to be returned, as similar to what happens with a LEFT OUTER JOIN, the columns from the non-matching table are returned as NULL. In this example, the product description for that group is returned as NULL.

2025-02-03

T-SQL 101: 106 Using LEFT OUTER JOIN

The next type of join that I want to discuss in these posts is a left outer join. We use this mostly, when we know that not all rows are matching.

For example, in the query shown above, I wanted to add up the ordered quantity values for all products. The problem is that if I used an INNER JOIN, I would only get products that match at least one order line.

2025-02-02

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