The Bit Bucket

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

SDU Tools: Languages in SQL Server T-SQL

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Applications often need to display a list of languages for users to choose from. To make that easy, we have now included a view called Languages.

The Languages view returns details of all the world’s languages, based on ISO 639-1. For each language, the view returns:

  • FamilyName
  • LanguageName
  • NativeLanguageName
  • ISO2CharacterCode
  • ISO3CharacterCode

The FamilyName indicates the group of languages that the language belongs to. The LanguageName is what we call it in English. The NativeLanguageName is what locals call the language. And then there are the 2 and 3 character codes from the ISO standard.

2025-02-08

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

SDU Tools: ExcelSerialToDateTime and DateTimeToExcelSerial

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Excel is fascinating in how it stores data. I have friends who joke that everything in Excel is a number or a string, and anything else you see is an illusion.

Date and time values in Excel are odd and use what most call a serial number when they’re stored. It’s common to need to import values from Excel into SQL Server and to export values to Excel.

2025-02-07

SQL Interview: 19: Store why an index exists

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

While developers don’t mind adding indexes to a database, most people are very nervous about ever removing them.

Tools let us see whether or not an index is being used, but it’s useful to know why the index was added in the first place.

2025-02-07

Cosmos Down Under show 13 with guest Rakhi Thejraj discussing auto-scaling in Cosmos DB is released

It’s been yet another big week for Down Under podcasts. I really enjoyed recording another new Cosmos Down Under podcast this afternoon. It’s now edited and released.

Show 13 features product group member Rakhi Thejraj.

Rakhi is one of the Cosmos DB product managers and is involved in supporting the elasticity, and ingestion features, plus security initiatives and AI customer reach outs.

I took this opportunity to discuss throughput provisioning and auto-scaling with her.

2025-02-06

SQL Interview: 18: Table Truncation and Foreign Keys

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Administration Level: Medium

Question:

You have two tables:

  • dbo.Customers
  • dbo.Orders

The dbo.Orders table has a column CustomerID that is declared as a foreign key to the CustomerID column in the dbo.Customers table. In the dbo.Customers table, CustomerID is the primary key.

2025-02-06

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

SQL Interview: 17: Using NULLIF

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

You see the following statement in a query that you are reviewing.

What is the purpose of the NULLIF function in this statement?

SELECT @a / NULLIF(@b, 0);

What was the author trying to achieve?

Answer:

The variable @a is being divided by the variable @b, but the variable @b might be zero. In that case, a divide by zero error would be returned.

2025-02-05

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