Review MySQL query classic example 2

#Query the student number and student name and average score of students whose average score is greater than or equal to 60 points
#Analyze, we can group the scores according to the student number, average the scores of the groups, and finally use the AVG function to average in the selected results Value and judge whether it is greater than or equal to 60
# General joint search

select student.sid,sname,avgScore from student,(
    select sid, AVG(score) as avgScore from score 
    GROUP BY sid
    HAVING AVG(score)> 60
    )r
where student.sid = r.sid;

#Left Connect

select s.sid,avgSocre,sname from(
select sid, AVG(score) as avgSocre from score  
GROUP BY sid 
HAVING AVG(score)> 60
)r left join 
(select student.sid, student.sname from
student)s on s.sid = r.sid;

#Right connection

select student.sid, student.sname, r.avgScore from student right join(
      select sid, AVG(score) AS avgScore from score
      GROUP BY sid
      HAVING AVG(score)> 60
)r on student.sid = r.sid;

#Query student information with grades in the socre table

select DISTINCT student.*
from student,score
where student.sid=score.sid

#Query the student number of all students, student names, total number of courses, and comprehensive results of all courses
#Joint query (students who have not selected courses will not be displayed)

#The join query can display students who have not selected courses (NULL for students who have not selected courses)

#查有
结果的学生信息#exists

select * from student 
where exists (select score.sid from score where student.sid = score.sid);

#in

select * from student
where student.sid in (select score.sid from score);

Guess you like

Origin blog.csdn.net/calm_encode/article/details/113530711