This blog is used to record the topics that I have done, the problems encountered and the summary when I am doing SQL server exercises.
Questions and Answers:
Databases involved in this exercise: stuManage
1. Write the functions implemented by the following SQL statements
(1) SELECT COUNT(*) AS Number of female students majoring in information management
FROM student
WHERE mno='100165' AND ssex='female'
统计了出信管专业(100165)女学生总人数。
(2)SELECT DISTINCT SUBSTRING(sname,1,1)
FROM student
查询出全部学生的姓。
(3)SELECT sno,sname,ssex,mno
FROM student
WHERE mno IN (‘100165’,’201148’,’100838’)
查询出mno是‘100165’或’201148’或’100838’的学生达到学号,姓名,性别和专业号。
(4)CREATE VIEW v_1
AS
SELECT ccno,mark
FROM student_course
WHERE sno =‘100212201’ AND mark >ANY
(SELECT mark FROM student_course
WHERE sno =‘100212208’ ) (5)SELECT DISTINCT s.sno,sname,dname FROM student AS s,department AS d, student_course as sc ,major as m WHERE s.mno=m.mno and d.dno=m.dno AND s.sno=sc.sno AND mark <60 (6)SELECT sno,sname,mname FROM student AS s,major AS m WHERE s.mno=m.mno AND s.sno IN (SELECT DISTINCT sno
创建一个视图v_1,该视图要求,学号为100212201的同学,
大于学号为100212208的同学所有科目的平均分的科目,
列出课程号和成绩。视图包含课程号和成绩两个字段。
找出课程成绩小于60分的同学,列出他们的学号,姓名和院系名(dname)。
FROM student_course
WHERE mark <60 )
找出课程成绩小于60分的同学,列出他们的学号,姓名和专业名。
2. According to the following requirements, write the corresponding query statement
(1) Inquire about the course selection of all male students, and ask to list the student number, name, course number, and score.
select student.sNo,student.sName,ccNo,Mark
from student_course,student
where student_course.sNO =student.sNO
and student.sSex='男';
(2) Create a view to display the total grades and the highest grades of all students, and it is required to list the student number, total grades and highest grades.
create view student_mark_view
as select sNo,sum(mark) as 总成绩,max(mark) as 最高成绩 from student_course group by sNo;
(3) Find out the students with the surname Zhang or students with "Qiu" in their names
select * from student
where sName like '张%' or sName like '%秋%';
(4) Query the average score, minimum score and maximum score of each course
select ccNo,avg(mark) as 平均分 ,min(mark) as 最低分, max(mark) as 最高分 from student_course group by ccNo;
(5) Find out the student numbers of students whose average score is greater than 80 and who have taken at least 2 courses.
select sNo from student_course group by sNo having AVG(mark)> 80
and count(ccNo)>=2;
(6) The student number, name and grade of the student whose elective course number is '010104' and whose grade is above 90
select student.sNo,sName,student_course.mark from student,student_course
where student.sNO = student_course.sNO
and ccNo='010104' and mark>90;
(7) Create a view to display the indirect predecessors of each course (that is, the predecessors of the predecessors)
select a.cno,a.cname,b.cpno as '间接先行课'
from course a,course b
where a.cpno = b.cno;
(8) Students whose grades in advanced mathematics courses are higher than Liu Chen's student number and grades
select student.sNO,student.sName,student_course.Mark
from student,student_course,course,course_class
where student.sno=student_course.sno
and student_course.ccNO = course_class.ccNo
and course_class.cNo = course.cno
and course.cName='高等数学'
and student_course.Mark>
(select student_course.mark from student,student_course
where student.sNO=student_course.sNO
and student.sName='刘晨'
and student_course.ccNO = course_class.ccNO);
(9) Find students in other departments who are younger than a student in the Department of Computer Science (that is, students who are younger than the oldest student in the Department of Computer Science)
select * from student,department,major
where student.mNO=major.mno
and major.dNO = department.dNO
and department.dName!='计算机系'
and DateDiff(yy,student.sBirth,getDate())<
(select Max(DateDiff(yy,student.sBirth,getDate())) from student
where student.mNo = major.mNO
and major.dNO = department.dNO
and department.dName!='计算机系'
);
(10) Query the names of students who have taken the three courses of "Database", "Finance" and "Statistics" at the same time. (requires EXISTS)
-- 法一,自己想的,创建视图只是为了简化表操作
create view three_course_view
as select student.sName,course.cName from student,student_course,course_class,course
where student.sNO = student_course.sNO
and student_course.ccNO = course_class.ccNo
and course_class.cNo = course.cNo;
-- 核心部分
SELECT sName FROM three_course_view a where cName in('数据库','金融学','统计学')
and exists(select 1 from three_course_view where cName in('数据库','金融学','统计学')
and sName=a.sName and cName<>a.cName) group by sName having count(cName)>=3 ;
-- 法二 ,咨询老师的,主要思路是通过group by 学号,把有选有数据库原理,金融学和计算机网络的学号筛选出来作为中间表 t
-- 并统计各个学号的学科科目数,如果选课科目等于3,则是同时选择了这三门科目的同学。
SELECT s.sno,s.sname
FROM student s
WHERE EXISTS (
SELECT * FROM
(SELECT sno,COUNT(sno) AS ccc
FROM student_course sc,course_class cc,course c
WHERE sc.ccno=cc.ccno AND cc.cno=c.cno
AND (c.cname='数据库原理与应用' OR c.cname='金融学'
OR c.cname='计算机网络')
GROUP BY sno)
t WHERE t.ccc =3 AND s.sNO = t.sNO )
(11) Query the names of students who have taken the three courses of "Database", "Finance" and "Statistics" at the same time. (Requires that EXISTS cannot be used)
create view three_course_view_noExists
as select student.sName,course.cName from student,student_course,course_class,course
where student.sNO = student_course.sNO
and student_course.ccNO = course_class.ccNo
and course_class.cNo = course.cNo;
select c1.sName,c1.cName,c2.cName
from three_course_view_noExists c1,three_course_view_noExists c2,three_course_view_noExists c3
where c1.sName=c2.sName
and c2.sName = c3.sName
and c1.cName ='数据库'
and c2.cName='金融学'
and c3.cName='统计学';
3. Thinking questions
(1) How to find the names of students ranked 5th to 10th in "Finance".
create view mark_rank_view as
select top 10 student.sName,mark,'金融学' as '科目' from student,student_course,course_class,course
where student.sNO = student_course.sNO
and student_course.ccNO = course_class.ccNo
and course_class.cNo = course.cNo
and course.cName='金融学'
order by mark desc ;
select top 10 sName from mark_rank_view where sName not in (select top 4 sName from mark_rank_view);
(2) Suppose there are two tables with the same structure, one is the operational data table A and the other is the historical data table B. Due to operation errors, some of the same data appears in the two tables. A delete?
根据题目意思,假设题中所指完全相等是指一行内的数据完全相同。
创建新一行数据,该行数据由两个表该行中所有字段连接成,
用where 判断两个表该新字段是否相等,如果相等,则根据表的id,删除该行。
Summarize
This exercise is somewhat difficult. Although I have studied databases before, I still think a lot about some topics, which shows that I am not very familiar with the operation of databases, especially the multi-table connection query. I still lack some experience. During the experiment, I encountered two difficult problems, namely 2.10 and 2.11, because it involves 4 table queries. When connected, the sql statement seems very long, and the logical relationship between them changes. It's hard to figure out, because I spent 3 or 4 hours trying to execute various queries when I finally did these two questions, but I didn't succeed. In the end, I first connected some fields of the four tables into a view, and then used the sql statement to query the view, and then I got the information required by the title.
After this exercise, I really learned a lot. I always thought that I was very proficient in database SQL statements. I thought this experiment should be very simple, but I didn't expect it took nearly a day to complete the experiment. Be conceited. In the future, I will learn SQL-related knowledge and think carefully about how SQL statements are deformed.
当然还有作者备注
@author Canlong