The following question was asked on Stack Overflow:
"I need to use Self Join on this table.
+------------+------+--------+
| Country | Rank | Year |
+------------+------+--------+
|France | 55 | 2000 |
+------------+------+--------+
|Canada | 30 | 2000 |
+------------+------+--------+
|Liberia | 59 | 2001 |
+------------+------+--------+
|Turkey | 78 | 2000 |
+------------+------+--------+
|Japan | 65 | 2003 |
+------------+------+--------+
|Romania | 107 | 2001 |
+------------+------+--------+
I need to use self join to get what countries has the same year as Turkey. Display the Country and year only."
In the answer chosen as correct, one of the recommended queries is:
SELECT DISTINCT a.Country, a.Year
FROM table1 AS a
INNER JOIN table1 AS b
on a.Year=b.Year
and b.Country='Turkey';
I don't get this query. Isn't it that a.Year=b.Year
always going to be true - as both the tables are the same? So what is the need of using it?
And isn't it that the above query will return only 'Turkey' in the country?
Please help me understand if I'm wrong.
Thanks a lot!