Background
Up until recently, I was one of the SQL Server developers adopting the bad habit that is known as the NOT IN clause. It is an easy way of finding data in one table, that does not exist in another. For this purpose, I thought using the NOT IN would help me conceptualise a query result, as well as help make it easier for someone else looking at the code. In fact, although the performance (within an execution plan) is OK, you can pull back incorrect results from the overall query.
The Problem
The NOT IN clause is problematic in only one, but VERY IMPORTANT way…….it DOES NOT include NULLS in the comparison table. Please see the example below:
Create two tables for NOT In Example:
Query results for both tables:
NOT In Query:
As you can see, 0 records were returned. We would expect the record (containing Striker, Andy Cole) in the NewFootyPlayers table to be returned. The NOT IN Clause is ignoring any comparisons on NULLS.
NOTE
Adding an additional ‘WHERE Position IS NOT NULL’ filter to the NOT IN clause would also give the same result but a lot of people will forget to add it and spend a substantial amount of time wondering why certain records are missing from their result set.
The Solution(s)
There are a number of clauses or SQL syntax that can be used instead of the NOT IN. although most do not have any major performance benefits, they actually return what is expected. The three examples below all return the one expected record:
All three return the below result, which we expected in the first place:
Recommended Solution
Whilst none of the solutions above cause major performance problems, there is one method that is better than the others. If we are working with hundreds of millions of records in both tables, using the NOT EXISTS is the most efficient query. Its performance is similar to NOT IN
and EXCEPT
, and it produces an identical plan, but is not prone to the potential issues caused by NULLs or duplicates.
I would be interested to see if anyone else has performance tested each query type and if there are better alternatives to NOT EXISTS. One thing I am certain on, however, is that no one should have to use the NOT IN clause.
Pareto Charts in Power BI and the DAX behind them
The Pareto principle, commonly referred to as the 80/20 rule, is a concept of prioritisation.
Apr
Databricks: Cluster Configuration
Databricks, a cloud-based platform for data engineering, offers several tools that can be used to
Apr
AI Assistance in Microsoft Fabric
The exponential growth of Large Language Models (LLMs) couples with Microsoft’s close partnership with OpenAI
Apr
10 reasons why it’s worth the effort to understand the value of your data
“If leaders really want to create a data driven culture, the journey starts with them!
Apr
Content Safety in Azure AI Studio
Azure AI Content Safety is a solution designed to identify harmful content, whether generated by
Apr
Model Benchmarks in Azure AI Studio
In the constantly changing field of artificial intelligence (AI) and machine learning (ML), choosing the
Apr
Celebrating International Women’s Day: from Classroom to Code
As we celebrate International Women’s Day, I want to share my journey of breaking stereotypes
Mar
Pretty Power BI – Adding Pagination to Bar Charts
Good User Experience (UX) design is crucial in enabling stakeholders to maximise the insights that
Feb