As much as I appreciate the community's help in improving posts, do NOT change the point/crux of the paragraphs. Information lost is opportunity missed.
- You should do a little more discovery on the structure and consistency of your data.
Ask questions such as 'Do you have Surrogate keys that identify the names uniquely?' or 'Does your first table containing ending spaces or Leading spaces?' If there is consistency in the naming standards, your solution can be elegant and simple. Otherwise, you may need to cleanse your data first before comparing tables.
- Character string comparisons are slow and usually require a perfect match (unless the
LIKE operator is used with a wildcard character. Col_1 LIKE 'Name%'). Furthermore, if possible, avoid using functions on both sides of your predicates (ON, WHERE, HAVING) as SQL will likely not be able to properly use Indexes on your columns and perform costly table/index scans.
Solution A using unique IDs on the table:
SELECT CustomerID, [First Name] AS First_Name
FROM TableA
INNER JOIN TableB ON CustomerID = BuyerID
WHERE TableA.First_Name = SUBSTRING(TableB.firstname, 1, LEN(TableA.First_Name) )
Easy, no? This uses a SARG in the WHERE clause and only compares the length of code that matters.
- Q. But I am not sure my data does not have duplicates or uses Surrogate keys
A. Then run the DISTINCT clause on your tables. DISTINCT is basically a GROUP BY, and eliminates M:M comparisons. Also, your code remains lean and can still utilize indexes properly and clearly.
WITH C AS (SELECT FirstName FROM TableA)
SELECT B.FirstName
FROM C
RIGHT OUTER JOIN (SELECT DISTINCT First_Name FROM TableB) B ON C.FirstName = SUBSTRING(B.First_Name, 1, LEN(C.FirstName) )
WHERE TableA.[Tie_Breaker] = TableB.[Tie_Breaker]
Note the RIGHT OUTER JOIN gives you NULLS for the rows that do not have a match in the right table (TABLEB). I left it here since you might like to compare what matches and mismatches you have between the tables (for quality assurane). Plus, you take advantage of simple functions like ISNULL or COALESCE and take pride that you have the leanest code in your department. :)
Special Thanks to @Matt for pointing out the need for tie breakers via the WHERE clause
Sources: MSDN, (n.d.) Predicates (Transact_SQL). Retrieved from MSDN