Review classic examples of MySQL query statements 4

#According to the average grade from high to low, display the grades and average grades of all the courses of all students

select * from score 
left join (
    select sid,avg(score) as avgScore from score 
    group by sid
    )r 
on score.sid = r.sid
order by avgScore desc;

#Query the highest score, lowest score and average score of each subject
# Display format: course ID, course name, highest score, lowest score, average score, pass rate, medium rate, excellent rate, excellent rate
# Pass is >=60, Moderate: 70-80, Excellent: 80-90, Excellent: >=90
#Require the output of the course number and the number of electives, and the query results are arranged in descending order by the number of people. If the number of people is the same, they are sorted in ascending order by the number of courses.

select score.cid,
max(score.score) as 最高分,
min(score.score) as 最低分,
AVG(score.score) as 平均分,
count(*) as 选修人数,
sum(case when score.score>=60 then 1 else 0 end )/count(*) as 及格率,
sum(case when score.score>=70 and score.score<80 then 1 else 0 end )/count(*) as 中等率,
sum(case when score.score>=80 and score.score<90 then 1 else 0 end )/count(*) as 优良率,
sum(case when score.score>=90 then 1 else 0 end )/count(*) as 优秀率 
from score
GROUP BY score.cid
ORDER BY count(*) DESC , score.cid ASC;


#Query the student's total score and rank, the total score is repeated and the ranking is not reserved #variables in SQL

set @crank=0;

#check sentence

select q.sid, total, @crank := (@crank +1) as 名次 from(
select score.sid, sum(score.score) as total from score
group by score.sid
order by total desc)q;

#Query student information, selected course information, selected course teacher information, selected course grade information

select * from student s,course c,teacher t,score sc where
s.sid = sc.sid and sc.cid = c.cid and c.tid = t.tid;

Guess you like

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