SQL Server › Hash Indexes & Hash Join Operations
What is a hash join, and how does SQL Server use it to combine two tables?
BeginnerAnswer
A hash join works by building a temporary hash table in memory from the smaller of the two input sets, based on the join column, and then scanning the larger input, checking each row against that hash table to quickly find matches, which works well when joining large, unsorted data sets without a useful index.
SELECT o.OrderId, c.Name
FROM Orders o
JOIN Customers c ON o.CustomerId = c.CustomerId
OPTION (HASH JOIN);Real-world example A reporting query joining two very large, unindexed tables uses a hash join, letting SQL Server efficiently match rows using an in memory hash table instead of comparing every row against every other row.