oracle 显示所有在所有科目中获得50分以上的学生的姓名

lnlaulya  于 2023-06-22  发布在  Oracle
关注(0)|答案(9)|浏览(214)

写一个查询,显示所有科目成绩超过50分的学生的姓名,按学生姓名升序排列。enter image description here
我已经使用了下面的查询,但没有得到想要的结果,因为它没有通过将student_id分组为整体来比较结果。任何人都可以在查询中提出任何更改。

select distinct student_name from student s join mark m on
s.student_id = m.student_id join subject sb
on m.subject_id = sb.subject_id
where m.value IN 
(select value from mark m1 join student s1
on m1.student_id = s1.student_id join subject sb1
on m1.subject_id = sb1.subject_id
where value > 50 group by s1.student_id,m1.value, sb1.subject_id)
order by 1;
fsi0uk1n

fsi0uk1n1#

select student_name
from student join mark using (student_id)
group by student_name
having min(value) > 50
order by student_name;

如果可能,应避免使用子查询。因此,如果检查一个学生的最低分数是否大于50,我们可以说他在所有科目中都取得了50分以上。

y53ybaqx

y53ybaqx2#

我不认为你需要这么复杂的子查询。您所需要做的就是找出学生的最低分数(INNER JOIN确保只考虑学生尝试过的科目)。
在student和student_id上的marks表之间执行简单的JOIN操作。我们在student_id上执行GROUP BY,并为每个学生获得minimum_marks。如果minimum_marks> 50,则意味着学生在所有科目中得分> 50。试试下面的方法,在MySQL中也可以(根据OP的Original标签):

SELECT s.student_name, MIN(m.value) AS minimum_marks 
FROM student s 
JOIN mark m ON s.student_id = m.student_id 
GROUP BY s1.student_id 
HAVING minimum_marks > 50 
ORDER BY s.student_name ASC

编辑根据Oracle(因为OP是后来编辑的),HAVING子句中的aliased fields/expressions are not allowed。修改后的查询如下所示:

SELECT s.student_name, MIN(m.value) AS minimum_marks 
FROM student s 
JOIN mark m ON s.student_id = m.student_id 
GROUP BY s1.student_id 
HAVING MIN(m.value) > 50 
ORDER BY s.student_name ASC
aiqt4smr

aiqt4smr3#

select student_name from student where student_id in
(select student_id 
    from (select student_id ,min(value) from mark group by student_id having min(value)>50)
)
order by student_name;

因为它是简单的通过制作子查询如上所示。

1.提取大于50的最小值并创建一个表,然后从student表中比较。
2.不使用任何Join操作

huus2vyu

huus2vyu4#

select distinct student_name from student,mark
where student.student_id = mark.student_id
having min(value) > 50
group by student_name
order by student_name;
jvlzgdj9

jvlzgdj95#

select student_name
from student s inner join mark m
on s.student_id= m.student_id
having min(m.value)>50
group by student_name
order by student_name asc;
v2g6jxz6

v2g6jxz66#

select s.student _name from student s join mark m 
on s.student_id = m.student_id
group by s.student_name
having min(m.value) > 50
order by s.student_name;
q5lcpyga

q5lcpyga7#

MySQL> select studentid, name,mark from students where mark>20;
yyyllmsg

yyyllmsg8#

select student_name from student
where student_id in (select student_id from mark group by student_id having min(value) > 50)
order by student_name;
e7arh2l6

e7arh2l69#

SELECT s.student_name FROM student s WHERE s.student_id NOT IN(SELECT m.student_id FROM mark m where m.student_id = s.student_id and m.value <= 50)ORDER BY s.student_name ASC;

相关问题