"Principles and Applications of Database" (Third Edition) book practice exercises after the book-Chapter 6

"Principles and Applications of Database" (Third Edition) book practice exercises after the book-Chapter 6


Data from the three tables given in the book

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Where the title is wrong

  • For all topics about VB courses, you can choose to change the Java courses in the table to VB courses, or change the VB courses in the topics to Java courses
    • I am here to change the VB class in the title to Java class to write
  • 18 Questions: Inquiry Course SelectionNumber of peopleThe maximum number of students and the number of courses for the first two students, including the parallel situation.
    • What is not smooth here should be: electiveNumber of doorsMost top two students

answer

Question 1. Query all the data in the student elective table.

SELECT * FROM SC

Insert picture description here

Question 2. Query the name and age of the students in the computer department.

SELECT Sname,Sage FROM Student WHERE Sdept='计算机系'

Insert picture description here

Question 3. Query the student ID, course number and score of students with a score between 70 and 80 points.

SELECT Sno,Cno,Grade FROM SC WHERE Grade BETWEEN 70 AND 80

Insert picture description here

Question 4. Query the names and ages of students in the computer department aged 18-20 and whose gender is "male".

SELECT Sname,Sage FROM Student 
	WHERE Sdept='计算机系' AND Sage BETWEEN 18 AND 20 AND Ssex='男'

Insert picture description here

Question 5. Query the highest score of the "c01" course.

SELECT MAX(Grade) AS c01的最高分 FROM SC WHERE Cno = 'c01'

Insert picture description here

Question 6. Query the maximum and minimum age of computer students.

SELECT MAX(Sage) AS 计算机系的最大年龄,MIN(Sage) AS 计算机系的最小年龄 FROM Student
	WHERE Sdept = '计算机系'

Insert picture description here

Question 7. Count the number of students in each department.

SELECT Sdept,COUNT(*) AS 学生人数 FROM Student GROUP BY Sdept

Insert picture description here

Question 8. Count the number of elective courses for each student and the total score of the exam, and display the results in ascending order of the number of elective courses (excluding students who did not choose courses).

SELECT Sno,COUNT(*) AS 选课门数,SUM(Grade) AS 总成绩 FROM sc GROUP BY Sno
	ORDER BY COUNT(*) ASC

Insert picture description here

Question 9. Query students with a total score of more than 200 points, list the student number and total score.

SELECT Sno,SUM(Grade) AS 总成绩 FROM SC
	GROUP BY Sno HAVING SUM(Grade)>200

Insert picture description here

Question 10. Query the names and departments of students who have chosen the "c02" course.

SELECT Sname,Sdept FROM Student S JOIN SC ON S.Sno = SC.Sno
	WHERE Cno = 'c02'

Insert picture description here

Question 11. Query the names, course numbers, and grades of students with scores above 80, and arrange the results in descending order of grades.

SELECT Sname,Cno,Grade FROM Student S JOIN SC ON S.sno = SC.sno
	WHERE Grade>80 ORDER BY Grade DESC

Insert picture description here

Question 12. To inquire about which students did not choose courses, ask to list the student number, name and department.

SELECT S.Sno,Sname,Sdept FROM Student S LEFT JOIN SC ON S.Sno = SC.Sno
	WHERE SC.Cno is NULL

Insert picture description here

Question 13. Count the number of elective courses for each course, list the course number and the number of elective courses (including courses that are not selected).

SELECT C.Cno,COUNT(SC.Sno) AS 选课人数 FROM Course C LEFT JOIN SC
	ON C.Cno = SC.Cno GROUP BY C.Cno

Insert picture description here

Question 14. Query the course name and starting semester of the courses offered in the same semester as "VB" and "Java".

/*题目应该是 查询与“Java"在同一学期开设的课程的课程名和开课学期。*/
SELECT Cname,Semester FROM Course
	WHERE Semester IN (
		SELECT Semester FROM Course WHERE Cname='Java')

Insert picture description here

Question 15. Query the name, department and age of students of the same age as Li Yong.

SELECT Sname,Sdept,Sage FROM Student
	WHERE Sage IN (
		SELECT Sage FROM Student WHERE Sname='李勇')

Insert picture description here

Question 16. Query the names and ages of the two youngest students in the computer department.

SELECT TOP 2 WITH TIES Sname,Sage FROM Student
	WHERE Sdept='计算机系'
	ORDER BY Sage ASC

Insert picture description here

17. Query title "VB" "the Java" the top two highest grades the student's name, and location-based "VB" "the Java" results, including the case of ties.

/*题目应该是 查询Java成绩最高的前两名学生的姓名,所在系和Java成绩,包括并列情况*/
SELECT TOP 2 WITH TIES Sname,Sdept,Grade
	FROM Student S JOIN	SC ON	S.Sno=SC.Sno
	JOIN Course C ON C.Cno=SC.Cno
	WHERE Cname = 'Java'
	ORDER BY Grade DESC

Insert picture description here

18. Course title query the number of the largest number of students in the top two door school enrollment numbers and the number of doors, including the case of ties.

/*题目应该是 查询选课门数组多的前两名学生的学号和选课门数,包括并列情况*/
SELECT TOP 2 WITH TIES Sno,COUNT(*) AS 选课门数
	FROM SC GROUP BY Sno ORDER BY COUNT(*) DESC

Insert picture description here

Question 19. Query the department with the largest number of students, and list the department name and number.

SELECT TOP 1 Sdept,COUNT(*) AS 人数 FROM Student
	GROUP BY Sdept ORDER BY COUNT(*) DESC

Insert picture description here

Question 20. Use subqueries to implement the following query:

(1) Query the name and department of the student who took the course "c01".

SELECT Sname,Sdept FROM Student
	WHERE Sno IN (SELECT Sno FROM SC WHERE Cno = 'c01')

Insert picture description here

(2) Query the student number, name, course number and score of students with a score of 80 or above in the Department of Mathematics.

SELECT Student.Sno,Sname,Cno,Grade FROM Student JOIN SC ON Student.Sno=SC.Sno
	WHERE Student.Sno IN(SELECT Sno FROM Student WHERE Sdept = '数学系') 
	AND SC.Sno IN(SELECT Sno FROM SC WHERE Grade > 80)

Insert picture description here

(3) Query the name of the student with the highest test score in the computer department.

SELECT Sname FROM Student JOIN SC ON Student.Sno=SC.Sno 
	WHERE Sdept = '计算机系' AND Grade = (SELECT MAX(Grade) FROM
		SC JOIN Student ON SC.Sno=Student.Sno WHERE Sdept = '计算机系') 

Insert picture description here

(4) Query the name, department, gender and score of the student with the highest test score in the data structure.

SELECT Sname,Sdept,Ssex,Grade FROM Student
	JOIN SC ON Student.Sno=SC.Sno
	JOIN Course ON SC.Cno=Course.Cno
	WHERE Cname='数据结构' AND Grade=(SELECT MAX(Grade)
		FROM SC JOIN Course ON SC.Cno=Course.Cno WHERE Cname='数据结构')

Insert picture description here

Question 21. Query the names and departments of students who have not selected the "VB" "Java" course.

/*题目应该是 查询没选“Java*课程的学生姓名和所在系*/
SELECT Sname,Sdept FROM Student 
	WHERE Sno IN(SELECT Sno FROM SC JOIN Course ON SC.Cno = Course.Cno
		WHERE Cname != 'Java')

Insert picture description here

Question 22. Query the names and genders of students who have not taken courses in the computer department.

SELECT Sname,Ssex FROM Student
 LEFT JOIN SC ON Student.Sno=SC.Sno
 WHERE Sdept='计算机系' AND SC.Sno IS NULL

Insert picture description here

Question 23. Query the name of the student with the lowest average score in the computer department exam and the name of the selected course.

SELECT Sname,Cname FROM Student
	JOIN SC ON Student.Sno=SC.Sno
	JOIN Course ON SC.Cno=Course.Cno
	WHERE  Student.Sno IN
	(SELECT TOP 1 WITH TIES Student.Sno 
		FROM Student JOIN SC ON Student.Sno=SC.Sno
		WHERE Sdept='计算机系'
		GROUP BY Student.Sno ORDER BY AVG(Grade) ASC)

Insert picture description here

Question 24. Query the course name, semester and credits of the course with the least number of students in the semester 1 to 5.

SELECT Cname,Semester,Credit FROM Course 
	WHERE Cno IN 
	(SELECT TOP 1 WITH TIES Cno FROM SC 
		WHERE Cno IN
		(SELECT Cno FROM Course
		 WHERE Semester
		 BETWEEN 1 AND 5)
		GROUP BY Cno ORDER BY COUNT(Sno) 
	)
	/*对1到5学期的课程号进行分组,根据学生数量排序(就是选课人数)*/

Insert picture description here

Question 25. Query the examination status of each student in the computer department, list the name, course name and test scores, and save the query results in a new table. The new table is named: Computer_Dept.

/*查询计算机系每个学生的考试情况,列出姓名、课程名和考试成绩,并将查询结果保存到一个新表中。新表名为: Computer_Dept*/
SELECT Sname,Cname,Grade INTO Computer_Dept
	FROM Student 
	LEFT JOIN SC ON Student.Sno=SC.Sno
	LEFT JOIN Course ON SC.Cno=Course.Cno
	WHERE Sdept='计算机系'

Insert picture description here
Insert picture description here

Question 26. Create a new table named test _t, whose structure is (COLI, COL2, COL3), where:

Insert picture description here

CREATE TABLE test_t(
	COL1 int,
	COL2 char(10) not NULL,
	COL3 char(10)
)
INSERT INTO test_t(COL2) VALUES('B1')
INSERT INTO test_t(COL1,COL2,COL3) VALUES(1,'B2','C2')
INSERT INTO test_t(COL1,COL2) VALUES(2,'B3')

Insert picture description here
Insert picture description here

Question 27. Delete the course selection records of students whose test scores are less than 50 points.

DELETE FROM SC WHERE Grade < 50

No less than 50 points
Insert picture description here

Question 28. Delete unselected courses.

DELETE FROM Course
	WHERE Cno NOT IN(SELECT Cno FROM SC)

Deleted two courses c03 computer network, c08 discrete mathematics
Insert picture description here
Insert picture description here

29. To remove a computer system problem "VB" "the Java" failing grade student of "VB" "the Java" elective record.

DELETE FROM SC
	FROM SC JOIN Student ON Student.Sno=SC.Sno
	JOIN Course ON Course.Cno=SC.Cno
	WHERE Cname='Java' AND Grade < 60 AND Sdept='计算机系'

No fail
Insert picture description here

Question 30. Delete the "VB" "Java" course selection records of the students with the lowest "VB" "Java" test scores .

DELETE FROM SC
	FROM SC JOIN Course ON Course.Cno=SC.Cno
	WHERE Cname='Java' AND Grade=(
		SELECT MIN(Grade) FROM SC
		JOIN Course ON Course.Cno=SC.Cno
		WHERE Cname='Java')

Deleted 9521103 c02 68 this record
Insert picture description here
Insert picture description here

Question 31. Increase the credits of all courses offered in the second semester by 2 points.

UPDATE Course SET Credit=Credit+2 
	WHERE Semester=2

Higher mathematics credits changed from 8 to 10
Insert picture description here
Insert picture description here

Question 32. Change the credit of VB Java course to 3 points.

UPDATE Course SET Credit=3
	WHERE Cname='Java'

Insert picture description here
Insert picture description here

Question 33. Increase the age of computer students by 1 year.

UPDATE Student SET Sage=Sage+1
	WHERE Sdept='计算机系'

Insert picture description here
Insert picture description here

Question 34. Add 5 points to the test scores of the "Computer Culture" course for information students.

UPDATE SC SET Grade=Grade+5
	FROM SC JOIN Course ON Course.Cno=SC.Cno
	JOIN Student ON SC.Sno=Student.Sno
	WHERE Sdept = '信息系' AND Cname='计算机文化学'

9521102 c01 score changed from 82 to 87
Insert picture description here
Insert picture description here

Question 35. Reduce the credits of the courses with the smallest number of courses by 1 point.

UPDATE Course SET Credit=Credit-1
	WHERE Cno IN (
		SELECT TOP 1 WITH TIES Cno
		FROM SC GROUP BY Cno
		ORDER BY COUNT(Sno) ASC)

The credits of database foundation, data structure, and operating system have changed from 4, 5, 4 to 3, 4, 3.
Insert picture description here
Insert picture description here
If the big brother finds an error, please correct me, thank you!

Published 318 original articles · Like 44 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/qq_43594119/article/details/105416187