The Bit Bucket

T-SQL 101: 123 Using Multiple CTEs in a Single T-SQL Query

I mentioned earlier that CTEs can be used to clean up or tidy more complicated queries. You will find people though, who really don’t like them at all. My aim here isn’t to argue for or against them, as it’s a complex topic, but to make sure you know how to write queries with them, and more importantly, to be able to read queries that use them. At this point, I want you to understand the syntax.

2025-03-02

Calculating sales volumes in general warehousing systems

After one of my recent posts, I was asked what an outer quantity was. This term is common in warehousing and logistics. So many business applications assume that you sell things by quantity, and that you just count the number in vs the number out.

But that’s not how it works.

A system that’s useful for these tasks, understands way more than that. The terms unit, inner, outer, shipper, layer, and pallet refer to different levels or units of packaging and organization in the supply chain. Here’s a breakdown of each term:

2025-03-01

PG Down Under show 4 on avoiding mistakes in PostgreSQL with guest Jimmy Angelakos is published!

I had the pleasure of recording a new PG Down Under podcast tonight. My guest was Jimmy Angelakos.

With over 25 years of experience in software architecture and a deep expertise in PostgreSQL, Jimmy is a recognized authority in the world of database systems. His career has spanned key roles at industry leaders like 2ndQuadrant and EDB, where he contributed to cutting-edge solutions and open-source advancements.

Background

Jimmy’s journey began with a Computer Science degree from the University of Aberdeen, and since then, he has worked with and significantly contributed to numerous open-source tools. His passion for the PostgreSQL community shines through as an active member of PostgreSQL Europe and a regular contributor to the PostgreSQL project. A sought-after speaker, he shares his insights at conferences and events dedicated to databases and open-source software.

2025-02-28

T-SQL 101: 122 Simplifying Queries with Common Table Expressions (CTEs) in SQL Server T-SQL

One of the challenges with subqueries is they can get very, very messy if you need to use them more than once. So for example, in the first query above, I’ve created a derived table called BulkProducts. But what if I needed to derive that same table again? Then I would end up with yet another copy of all that code. (And obviously it might be far more complex)

A more common problem is that in a SELECT statement, I might be selecting subqueries but every single time I might be needing to calculate a sub-query to get the values.

2025-02-28

SDU Tools: List and Retrust Untrusted Foreign Key Constraints in SQL Server

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. I do a lot of reviewing of database designs and one thing I always check for is foreign key constraints that are not currently trusted. So we added a tool that can help to find these. It’s called ListUntrustedForeignKeys.

The procedure takes three parameters.

@DatabaseName is the database to process @SchemasToList is a comma-delimited list of schema names to process @TablesToList is a comma-delimited list of table names to process

2025-02-27

T-SQL 101: 121 Using VALUES Clauses as Row Constructors in SQL Server T-SQL

SQL Server 2008 and later support a VALUES clause. We can use this to construct a table on the fly.

In the example above, you can pretty much ignore the outer query as I just put it there to select from the table that was derived in the VALUES clause.

With VALUES, I put a list of rows that I want to include. They each must have the same number of columns. There can be up to 1000 rows at a time.

2025-02-26

SDU Tools: List and Retrust Untrusted Check Constraints in SQL Server

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. I do a lot of reviewing of database designs and one thing I always check for is check constraints that are not currently trusted. So we added a tool that can help to find these. It’s called ListUntrustedCheckConstraints.

The procedure takes three parameters.

@DatabaseName is the database to process @SchemasToList is a comma-delimited list of schema names to process @TablesToList is a comma-delimited list of table names to process

2025-02-25

T-SQL 101: 120 Nested vs Correlated Subqueries in SQL Server T-SQL

In the previous T-SQL 101 post, I mentioned that some subqueries depend upon the outer query and others don’t. That’s the distinction between what are called nested and correlated subqueries.

Nested Subquery

I mentioned that you can also use a subquery to return a table of values. In the first example above, I’ve selected some columns from order lines and then said inner join to a derived table of products where their outer quantity is greater than 24.

2025-02-24

SQL Interview 23: Logical issues related to ISNULL in SQL Server T-SQL

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:

A query included within a stored procedure, has a predicate written like this:

WHERE ISNULL(ProductSize, '') = ISNULL(@ProductSize, '')

A problem has been identified with this predicate. The system needs to be able to differentiate between NULL values for product size and blank strings. Currently both are treated as though they are the same.

2025-02-23

T-SQL 101: 119 Checking for Existence with EXISTS Subqueries

Sometimes what we’re interested in is just whether or not a row exists.

In the example shown, I’m saying I want to select product ID and description from the products table where there is at least one order line that has the picked quantity less than the ordered quantity. That means that someone ordered a certain number, but for some reason, we didn’t pick (i.e. got ready for shipment) the quantity that they wanted.

2025-02-22