Some writing about SQL Exercise 2. . . Query courses with more than 2 electives and scores above 60 points. Query elective students who have taught all courses taught by Teacher Zhang. Calculate and fill in the total credits obtained by the students

10-1 Query for students who did not take the'C language' course (10 points)
This topic requires writing SQL statements, retrieve the records of students who have not taken the'C language' course, and the output result set is sorted in ascending order of student number.

Tip: Please use the SELECT statement to answer. Please use not exist.

select sno as 学号,sname as 姓名 from stu  
where  sno  not in
(select distinct sno from sc where  
  not exists(select * from cou where cou.cno = sc.cno and cname != 'C语言') )  
			 order by sno asc;

10-2 Query courses taken by S001 students but not selected by S003 students (10 points)
This topic requires the preparation of SQL statements, and retrieve the number of the courses taken by students whose middle school number is S001 but not taken by students with S003 student number in the sc table.

Tip: Please use the SELECT statement to answer. MySQL does not allow the use of the except statement.

select cno as 课程号 from sc
where cno not in(
select s1.cno as 课程名  from sc s1 join sc s2
on s1.cno = s2.cno
where
(s1.sno = 's001' and s2.sno = 's003')) and sno = 's001';

10-4 Query for students with an average score of over 80 (10 points)
This question requires writing SQL statements to query the names of students with an average score of over 80.

Tip: Please use the SELECT statement to answer.

select  sname  from stu 
where sno in
(
select sno from sc group by sno having avg(grade) >= 80);

10-5 Query the students who have taken all the courses taught by Teacher Zhang (10 points)
This topic requires the preparation of SQL statements to query the students who have taken all the courses taught by Teacher Zhang.

Tip: Please use the SELECT statement to answer.

select sname
from stu
where not exists
(select * 
from cou
where not exists
(select *
from sc 
where sc.sno=stu.sno and sc.cno=cou.cno) and cou.teacher = '张老师')

10-6 Calculate and fill in the total credits obtained by the students (10 points).
This question requires writing an UPDATE statement, calculating the total credits each student has obtained and filling in the totalcredit field in the stu table.

Among them, the total credit is the sum of the credits of the elective courses passed by each student. Note: Only the elective scores of 60 points or more can get the credits of the course. The credits of each course are in the credit field in the cou table.

update stu,(select sno sno,sum(credit) total 
from
(select sc.sno sno,case when sc.grade>=60 then credit else NULL end credit
from stu
join sc on sc.sno = stu.sno
join cou on sc.cno = cou.cno
group by sc.sno,credit,grade) nstu1
group by sno) nstu2
set stu.totalcredit = nstu2.total
where stu.sno = nstu2.sno;

This question feels a bit too high when I put it here. I will wait for the later when then.

10-7 Through the book table and borrowing table, query the borrowing situation of the book. The required results include the following columns: account number, barcode, book title and borrowing date (10 points)
This topic requires the preparation of SQL statements, through the book list and borrowing Table, query the borrowing situation of the book, the required results include the following columns: account number, barcode, book title and borrowing date

Tip: Please use the SELECT multi-table query method.

select 借阅.账号,借阅.条形码,图书.书名,借阅.借书日期 from 图书 join
借阅
on 图书.条形码 = 借阅.条形码

10-8 Query the name of the oldest student in the software engineering major (10 points)
This topic requires writing SQL statements to query the name of the oldest student in the software engineering major.

Tip: Please use the SELECT statement to answer.

select sname from stu 
where mno = (select mno from major where mname = '软件工程')
and birdate = 
(select min(birdate) from stu)

10-9 Query for students who took the "C language" course but did not take the "Data Structure" course (10 points)
This topic requires writing SQL statements, and the query takes the "C Language" course but does not take the "Data Structure" course The name of the student.

Tip: Please use the SELECT statement to answer.

select sname from stu where sno in(
select distinct sno from sc where sno in(
select sno from sc 
where sno not in
(select sno from sc where cno = 'c003')  and cno = 'c002'))

(select sno from sc where cno ='c003') and cno ='c002')) can also be used to find
cno equal to "C language" and not equal to "data structure".

10-10 Query for students who have more than 2 elective courses and scores above 80 points (10 points)
This question requires writing SQL statements to query the names, majors, and totals of students who have more than 2 elective courses and scores above 80 points credit.

Tip: Please use the SELECT statement to answer.

select sname 姓名,mno 专业,sum(credit) 总学分 from stu join
sc on stu.sno = sc.sno join cou
on sc.cno = cou.cno
group by sname,mno
having count(sc.cno) >= 2 and min(grade) >=80;

10-11 Query courses with more than 2 electives and scores above 60 points (10 points)
This question requires the preparation of SQL statements, query the course name and highest score of courses with more than 2 electives and scores above 60 points , Minimum grade and average grade.

Tip: Please use the SELECT statement to answer.

select sc.cno 课程号,cname 课程名,max(grade) 最高成绩,min(grade) 最低成绩,avg(grade) 平均成绩 from sc join
cou on sc.cno = cou.cno
group by sc.cno,cou.cname having count(sc.cno) > 2 and min(grade) >= 60 and count(*) = count(grade )
;

Guess you like

Origin blog.csdn.net/zheziu/article/details/111563788