Article directory
On the basis of Experiment 4, this article adds the knowledge points of orderby, aggregate function, groupby, and multi-table query. Compared with the previous experiment, the difficulty has become greater, and there are more nested expressions. Gradually start nesting dolls... In fact, it
can Split it as a positive phrase, and then write it into an SQL statement. For example, if you query the grades of a student whose age is greater than 20 , then the student's grades will beCenter language, and the age is greater than 20 asmodifier, put the central language in select ...from
between, and analyze it by analogy.
- Query 'C language' or 'Database' student number, name, department, elective course number and grade;
- Center language: put Student.sno, sname, sdept, Course.cno, SC.grade in front
- Since the content of the query involves three tables, the three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: the course is 'C language' or 'Database' = "to query 'C language' or 'Database' in the course name
select Student.sno,sname,sdept,Course.cno,SC.grade
from Student,Course,SC
where Student.sno=SC.sno and Course.cno=SC.cno
and Course.cname in ('C language','Database');
- Query the details of all students with missing grades;
- Center language: student's detailed situation Student.*
- Merge Student and SC two tables Student.sno=SC.sno
- Modifier: grade is missing grade is null
select Student.*
from Student,SC
where Student.sno=SC.sno and grade is null;
- query for information about all students who are not of the same age as 'Lisi' (assuming the name is unique);
- Center Language: Student Information Student.*
- Extract information from table Student
- Modifier: different from 'Lisi' age; extract age as head, Lisi's age as modifier
select Student.*
from Student
where sage not in (select sage from Student where sname='Lisi');
- Query the student number, name and average grade of the students whose average grade of the selected course is greater than the average grade of 'Lisi'
- Center language: student number, name and average grade Student.sno, sname, avg(SC.grade)
- Merge Student and SC two tables Student.sno=SC.sno
- Modifier: the average grade avg(Grade) of the course
- Sub-statement: 'Lisi''s average grade = "check Li Si's grade, nest a sub-statement: select avg(grade) from SC,Student where sname='lisi'and Student.sno=SC.sno
select Student.sno,sname,avg(grade)
from Student ,SC
where Student.sno=SC.sno
group by Student.sno,sname
having avg(Grade)>(select avg(grade)from SC,Student where sname='lisi'and Student.sno=SC.sno)
Since there is an aggregate function avg in the conditional statement, all use group by ... having
- count sum avg max min
- It cannot appear after the where statement, use group by and having
- List the credits obtained by the students in the order of their student number, name, department, and credits they have taken.
Among them, the credits taken are the sum of the credits of the courses that have passed the examination;
- Central language: student number, name, department, credits earned
- The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: Sum of credits for courses already passed
select Student.sno,sname,sdept,sum(ccredit)
from Student,SC,Course
where Student.sno=SC.sno and SC.cno=Course.cno
and grade>60 group by student.sno,sname,sdept;
- Query the student number, name, department and grades of students who only take one course;
- Center language: the student's student number, name, department and grade
- Involving two table contents, connect the two tables: Student.sno=SC.sno
- Modifier: Take only one course
- Sub-Sentence Head: Course
- Sub-statement modifiers: only take one course
select Student.sno,sname,sdept,grade
from Student,SC
where Student.sno=SC.sno
and SC.sno in (select sno from SC group by SC.sno having count(distinct cno)=1);
- Find the student number, name and course number of students who have taken at least one course that is the same as 'Lisi';
- Center language: student number, name and course number of the student
- The two tables are merged according to the index Student.sno=SC.sno
- Modifier: Take at least one elective course same as 'Lisi'
- Sub-Sentence Head:Students of the same course
- Sub-statement modifier: 'Lisi' elective course
select Student.sno,sname,cno
from Student,SC
where Student.sno=SC.sno
and SC.sno in
(select SC.sno from SC,Student where cno in
(select cno from Student,SC where sname='lisi' and Student.sno=SC.sno));
- Basic information of students who take at least 'C language' or 'Database' courses;
- Include only one or both of:
- Center language: basic information of students
- The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: Take at least 'C language' or 'Database' courses
- Sub-Sentence Head: Course
- Sub-statement modifier: optional 'C language' or 'Database'
select Student.sno,sname,sdept,SC.cno,cname,grade
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.cno in
(select SC.cno from SC,Course where (cname='C language' or cname='Database') and SC.cno=Course.cno);
- Both courses include:
- Center language: basic information of students
- The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: Take at least 'C language' or 'Database' courses
- Sub-Sentence Head: Course
- Sub-statement modifiers: elective 'C language' and 'Database'
select Student.sno,sname,sdept,SC.cno,grade
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.sno in (select SC.sno from SC,Course where (cname='C language' or cname='Database')
and SC.cno=Course.cno group by SC.sno having count(SC.cno)>=2);
- List the details of all courses taken, including course number, course name, student number, name and grades
- Center language: course number, course name, student number, name and grade
- The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: Courses are taken
select Course.cno,cname,Student.sno,sname,grade
from Student,SC,Course
where Student.sno=SC.sno and SC.cno=Course.cno
order by Course.cno;
- Query the course number and course name of a course that is only taken by one student
- Center language: course number, course name
- The two tables are merged according to the index Course.cno=SC.cno
- Modifier: Courses taken by only one student
select SC.cno,cname
from SC,Course where Course.cno=SC.cno
group by SC.cno,cname having count(SC.cno)=1;
11. Use a nested query to list the student numbers and names of students who have taken the course 'Data structure'
- Center language: student number and name
- The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
- Modifier: 'Data structure' course taken
- Sub-Sentence Head: Course
- Sub-statement modifier: elective 'Data structure'
select Student.sno,sname
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.cno in
(select SC.cno from SC,Course where SC.cno=Course.cno and Course.cname='Data structure');
- Use a nested query to query the student name, age, and department of a student younger than AI in another department;
- Head: student name, age, and department
- Select from table Student
- Modifier 1: other departments
- Modifier 2: the age of a student younger than AI
select sname,sage,sdept
from Student
where sage<any (select sage from Student where sdept='AI') and sdept<>'AI';
reference article
https://blog.csdn.net/qq_43776450/article/details/95965866
https://blog.csdn.net/qq_43717736/article/details/107722502
https://max.book118.com/html/2017/0326/97190702.shtm