2015-02-05 64 views
0

我使用SQL-server.I有一个表,看起来像内连接VS其他一些方法

StudentName Class score 
Jim   a1  80 
Ann   a1  83 
Bill   a2  90 

我想选择的学生,其成绩是在他/她的班级平均分以上。这里是我的代码:

Select a.StudentName 
From Table a 
inner Join Table b 
On a.class=b.class 
where a.score>(select avg(b.score) From b 
group by class); 

内部联接方法看起来奇怪,me.Is它正确吗?有没有更好的方法来达到这个目的?

+0

你有一个表中的所有学生。你为什么需要加入?联接用于组合两个(或更多)表。 – stupidstudent 2015-02-05 20:21:01

回答

2

你靠近,但你可以限制你的子查询,并采取了JOIN:

Select a.StudentName 
From Table a 
where a.score > (
    select avg(b.score) score 
    From Table b 
    where b.class = a.class); 
+0

感谢您的回复。我不需要加入吗?我应该定义b吗? – user4441082 2015-02-05 20:26:48

+0

不;你可以通过加入子查询来做到这一点,但这是更清洁的恕我直言。而b在子查询中定义。 – 2015-02-05 20:29:07

0

如何:

​​
+0

如果我使用Oracle,我可以使用;请用xxx()定义? – user4441082 2015-02-05 20:28:49

+3

我不知道。您如何在另一个带有Oracle标签的问题中提出这个问题? – Elliveny 2015-02-05 20:33:43