SQL select table 中的实例,如果它不再存在且具有较晚的年份值

SQL select instance in table if it does no longer exist with later year value

给出以下table结构:

year team_ID team
2011 1 Manchester
2011 2 Bayern
2011 3 Madrid
2012 1 Manchester
2012 2 Bayern
2012 4 Chelsea

Objective 属于 select 2011 年进入 table 但在 2012 年被踢出局的球队。 在这种情况下,只有马德里有 2011 年而不是 2012 年,因此它将是唯一的 selected。

预期输出为:

team
Madrid

感谢您的帮助!

你应该使用相关的exists,比如

select team
from t
where year=2011
and not exists (select * from t t2 where t2.year = 2012 and t2.team = t.team);