I have a table instructors
with the following strcture/data:
-----------------
|id | name |
-----------------
|1 | saad |
|2 | imran |
-----------------
I want to do a join with the same table without returning duplicate results.
If i do a simple join, the query returns 4 rows [saad, saad], [saad, imran], [imran, imran], [imran, saad]
.
And if I add a where clause it returns 2 rows [saad, imran], [imran, saad]
.
However, I only want to return 1 row, (i.e. if [saad, imran]
was already returned, the query shouldn't return [imran, saad]
).
Here's my query thus far:
SELECT DISTINCT
i1.name AS name1,
i2.name AS name2
FROM
instructors i1, instructors i2
WHERE
i1.name != i2.name
ORDER BY
name1, name2