I’m new to SQL and I don’t understand why you need a <>
condition in a self join like below. I get that a join with an equal to condition won’t return any results but I can’t get my head around the need for <>
in this case.
Please let me know if I need to provide any other info to answer this.
SELECT DISTINCT
t1.number AS num1,
t2.number AS num2
FROM transportation_numbers t1
JOIN transportation_numbers t2 ON t1.index <> t2.index
WHERE
t1.number < t2.number AND
t1.number * t2.number > 11
2
Answers
This means that each row in a table is joined to itself and every other row in that table.
I’ve used it many times in things like self joins across one column that is not a unique identifier/key and then use the
<>
to prevent joining the same ID to itself.Something like