我的任务是:查找只有同年级朋友的学生的姓名和年级。
我的桌子:
高中生
ID name grade
1510 Jordan 9
1689 Gabriel 9
1381 Tiffany 9
1709 Cassandra 9
1101 Haley 10
1782 Andrew 10
1468 Kris 10
1641 Brittany 10
1247 Alexis 11
1316 Austin 11
1911 Gabriel 11
1501 Jessica 11
1304 Jordan 12
1025 John 12
1934 Kyle 12
1661 Logan 12
朋友
ID1 ID2
1510 1381
1510 1689
1689 1709
1381 1247
1709 1247
1689 1782
1782 1468
1782 1316
1782 1304
1468 1101
1468 1641
1101 1641
1247 1911
1247 1501
1911 1501
1501 1934
1316 1934
1934 1304
1304 1661
1661 1025
1381 1510
1689 1510
1709 1689
1247 1381
1247 1709
1782 1689
1468 1782
1316 1782
1304 1782
1101 1468
1641 1468
1641 1101
1911 1247
1501 1247
1501 1911
1934 1501
1934 1316
1304 1934
1661 1304
1025 1661
我的查询:
select name, grade from highschooler
where id in
( select distinct id1
from friend out
where not exists
(select id1
from friend
where
(select grade from highschooler where id = out.id1) <>
(select grade from highschooler where id = out.id2)
)
)
我的结果:
Jordan 9
Gabriel 9
Tiffany 9
Cassandra 9
Haley 10
Andrew 10
Kris 10
Brittany 10
Alexis 11
Gabriel 11
Jessica 11
Jordan 12
John 12
Kyle 12
Logan 12
预期的查询结果:
Jordan 9
Brittany 10
Haley 10
Kris 10
Gabriel 11
John 12
Logan 12
我用 Tiffany 仔细检查了我的代码:
1381 Tiffany 9 1247 Alexis 11
1381 Tiffany 9 1510 Jordan 9
我的相关子查询有问题。你能告诉我什么是错的吗?