Beginner

666. JOIN with NOT Equal

Understand JOIN conditions that intentionally exclude equal values.

📘 Lesson 666 of 728 ⏱ 14 min 🎯 Advanced

A non-equality JOIN can use <> or != to match rows where specified values are different.

Although uncommon, inequality JOINs can be useful for comparison and exception-analysis scenarios.

Compare employees from the same department who have different salaries.

SQL
SELECT
    e1.EmployeeName AS Employee1,
    e2.EmployeeName AS Employee2
FROM Employees e1
INNER JOIN Employees e2
    ON e1.DepartmentID = e2.DepartmentID
   AND e1.Salary <> e2.Salary
   AND e1.EmployeeID < e2.EmployeeID;

Used in comparison analysis, exception reporting, duplicate investigation, and data-quality checks.

  • Inequality JOINs use <> or != depending on the SQL dialect.
  • They can generate many combinations.
  • Self JOINs often use inequality conditions.
  • A unique-row condition can prevent duplicate pairs.
  • Use carefully on large datasets.
Can a JOIN use <>?

Yes. A JOIN condition can use inequality operators when the business logic requires it.

Where are inequality JOINs commonly useful?

They are useful for row comparisons, exception analysis, and certain range or relationship problems.

Have you finished this lesson?

Introduction to SQL – All Lessons