I'm fine, thank you for playing sql

Super classic SQL practice questions, after completing these, your SQL will pass the test

Original URL: https://blog.csdn.net/flycat296/article/details/63681089

# 1. Query the information and course scores of students with higher grades in the "01" course than the "02" course
select * from (select * from SC where c = '01') A
left join (select * from SC where c = '02') B
on A.s = B.s
where A.score > B.score;

# 2. Query the student number, student name and average grade of students whose average grade is greater than or equal to 60
select sum(A.score), count(*), sum(A.score)/count(*) as pingjun from SC A
left join Student B
on A.S = B.S
group by B.S;


# 4. Query the student number, student name, total number of courses, and total grades of all courses of all classmates (null is displayed if there is no grade)
select A.S, A.Sname,count(DISTINCT B.C) ,sum(B.score) from Student A
left join SC B
on A.S = B.S
GROUP BY A.s;


#6. Query the information of students who have studied with teacher "Zhang San"
select * from SC B
where B.C in( select A.C from Course A where A.T = '02');

select * from SC B
RIGHT JOIN (select A.C from Course A where A.T = '02') C
on B.C = C.C;

# 7. Query the information of students who did not learn all the courses (having clause, having is a filter group and where is a filter record, you must use having with group by.)
select * from (select A.S , count(*) as n from SC A group by A.S ) B
where B.n < 3;
# having
select A.S , count(*) as n from SC A group by A.S Having count(n) < 3

#8 Query the information of students who have at least one course that is the same as that of the student whose student number is "01"
select * from SC A
where A.C in ( select C from SC B where B.S='01') and A.S <> '01'
group by A.S


#9 Query the information of other students who have the exact same course as the classmate "01"
select * from (select s,group_concat(c order by c) gc from SC group by s) a
join Student s on a.s=s.s where a.gc=(select group_concat(c) from SC where s=6)

#-11. Check the student number, name and average grade of students who failed two or more courses   
select C.S,AVG(score)  from  SC C where C.S in (select A.s from SC A where A.score< 60
GROUP BY S
HAVING count(*) >2)
GROUP BY C.S


-- 13. (Static writing) Display the grades and average grades of all students in all courses from high to low
select S,
max(case C when '01' then score else 0 end) as course one,
max(case C when '02' then score else 0 end) as 课程二,
max(case C when '03' then score else 0 end) as course three, AVG(A.score) average score from SC A
GROUP BY A.S ORDER BY 平均分 desc
   
-- 14. Query the highest, lowest and average scores of each subject:
-- Displayed in the following formats: course ID, course name, highest score, lowest score, average score, pass rate, average rate, excellent rate, excellent rate
 -- Pass is >=60, Moderate is: 70-80, Excellent is: 80-90, Excellent is: >=90
-- It is required to output the course number and the number of electives. The query results are arranged in descending order of the number of people. If the number of people is the same, they are arranged in ascending order of the course number.
select AC as course ID, B.Came course name, count(*) number of electives,
sum(case  when score>=60 then 1 else 0 END)/count(*) as 及格率,
sum(case  when score>=70 AND score< 80 then 1 else 0 END)/count(*) as 中等率,
sum(case  when score>=80 AND score< 90 then 1 else 0 END)/count(*) as 优良率,
sum(case  when score>=90  then 1 else 0 END)/count(*) as 优秀率
from SC A
JOIN Course B  on A.C = B.C  
group by A.C
ORDER BY count(*) desc, A.C desc;
 
-- 15. Sort by the scores of each subject, and display the ranking, and keep the ranking vacancy when the Score is repeated
-- 15.2 Sort by the scores of each subject, and display the ranking, and combine the rankings when the Score is repeated
-- Reference: https://blog.csdn.net/a9925/article/details/76804951
select @rownum := @rownum + 1 no parallel sorting,

score from SC ,(SELECT @rownum := 0) t   
where c = 01
ORDER BY score desc;

-- 18 Query the top three records of each subject (using the partition function of sql_server)
Partition function









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812667&siteId=291194637