A Join A Day – Equi-Join vs. Nonequi-Join

2012-12-18 - A Join A Day, Fundamentals, General, Series

Introduction

This is the eighteenth post in my A Join A Day series about SQL Server Joins. Make sure to let me know how I am doing or ask your burning join related questions by leaving a comment below.

So far all examples in this series have joined two tables on one or more columns using the "equals" comparison. If all columns in the ON clause are matched on equality the join is called "Equi-Join". If one or more columns are using an inequality comparison, the join is called "Nonequi-Join".

(Non)Equi-Join Example

For an example let's reuse our tables TblA and TblB:

setup for all-cases join example

The type of query we have looked at several times by now is an Equi-Join:

[sql] SELECT *
FROM dbo.TblA AS a
JOIN dbo.TblB AS b
ON a.TblA_Val = b.TblB_Val;
[/sql]

This returns only rows where a.TblA_Val and b.TblB_Val are equal:

a simple equi-join example

Now let's alter the query a little:

[sql] SELECT *
FROM dbo.TblA AS a
JOIN dbo.TblB AS b
ON a.TblA_Val > b.TblB_Val;
[/sql]

Now all combinations of rows from TblA and TblB are returned where the value of a.TblA_Val is greater than the value of b.TblB_Val:

a simple nonequi-join example

Nonequi-Joins are often used in the context of Self-Joins. With a Nonequi-Join you can do things like calculate a running total:

[sql] SELECT a1.TblA_Val, a1.TblA_Desc, SUM(a2.TblA_Val) SumVal
FROM dbo.TblA AS a1
JOIN dbo.TblA AS a2
ON a1.TblA_Val >= a2.TblA_Val
GROUP BY a1.TblA_Val, a1.TblA_Desc;
[/sql]

This query calculates for each row in TblA the sum of all the TblA_Val values that are smaller or equal than the current rows TblA_Val value.

calculate the running total with a nonequi-join

If you are working with SQL Server 2012 however, you can use window functions to do running total calculations much more efficient. A good introduction to windowing functions you can find in this book by Itzik Ben-Gan: Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions

(Non)Equi-Join Syntax

Any join that compares one or more columns in the on clause and uses only the "equals" comparison is an Equi-Join. This is independent of the logical join type. So you can have an Inner Equi-Join as well as a Right Anti Semi Equi-Join. Any join that uses another comparison operator for at least one column is a Nonequi-Join.

You can have a mixed situation too, where some columns use the "equals" comparison and other columns use another comparison. Those are commonly also called Nonequi-Joins even though they look more like an Equi-Join with an additional filter to SQL Server.

(Non)Equi-Join Operator

There is no special operator for Equi-Joins or Nonequi-Joins either. All three algorithms can handle Equi-Joins and mixed joins. However, with one special case exception that we will talk about in a few days, only the Nested Loop operator can handle plain Nonequi-Joins.

Summary

A join that is using only the "equals" comparison in the join condition is called Equi-Join. A Join that has at least one comparison in the join condition that is not an "equals" comparison is called a Nonequi-Join. Nonequi Joins are often used for calculations of running totals and similar metrics.

A Join A Day

This post is part of my December 2012 "A Join A Day" blog post series. You can find the table of contents with all posts published so far in the introductory post: A Join A Day – Introduction. Check back there frequently throughout the month.

Categories: A Join A Day, Fundamentals, General, Series

Leave a Reply