Beginner

697. JOIN with TRIM

Use TRIM to remove unwanted spaces before comparing JOIN values.

📘 Lesson 697 of 728 ⏱ 13 min 🎯 Intermediate

TRIM removes leading and trailing whitespace from a string.

Imported or manually entered data can contain extra spaces that prevent otherwise valid JOIN matches.

Match department codes after removing unwanted spaces.

SQL
SELECT
    e.EmployeeName,
    d.DepartmentName
FROM Employees e
INNER JOIN Departments d
    ON TRIM(e.DepartmentCode)
     = TRIM(d.DepartmentCode);

Used in data imports, Excel-to-database integrations, migration projects, and data cleaning.

  • TRIM removes surrounding whitespace.
  • It can improve matching of dirty text data.
  • Internal spaces are not normally removed.
  • Function-based JOINs can affect performance.
  • Cleaning data before storage is preferable when possible.
What does TRIM do?

TRIM removes leading and trailing whitespace from a string.

Why might TRIM be useful in a JOIN?

It can make values match when unwanted spaces were introduced during data entry or import.

Have you finished this lesson?

Introduction to SQL – All Lessons