[Database] Experiment 5 Comprehensive database query|Multi-table query, aggregate function, orderby, groupby

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 ...frombetween, and analyze it by analogy.


  1. Query 'C language' or 'Database' student number, name, department, elective course number and grade;
  1. Center language: put Student.sno, sname, sdept, Course.cno, SC.grade in front
  2. 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
  3. 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');

insert image description here


  1. Query the details of all students with missing grades;
  1. Center language: student's detailed situation Student.*
  2. Merge Student and SC two tables Student.sno=SC.sno
  3. Modifier: grade is missing grade is null
select Student.* 
from Student,SC 
where Student.sno=SC.sno and grade is null;

insert image description here


  1. query for information about all students who are not of the same age as 'Lisi' (assuming the name is unique);
  1. Center Language: Student Information Student.*
  2. Extract information from table Student
  3. 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');

insert image description here


  1. 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'
  1. Center language: student number, name and average grade Student.sno, sname, avg(SC.grade)
  2. Merge Student and SC two tables Student.sno=SC.sno
  3. Modifier: the average grade avg(Grade) of the course
  4. 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

insert image description here


  1. 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;
  1. Central language: student number, name, department, credits earned
  2. The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
  3. 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;

insert image description here


  1. Query the student number, name, department and grades of students who only take one course;
  1. Center language: the student's student number, name, department and grade
  2. Involving two table contents, connect the two tables: Student.sno=SC.sno
  3. Modifier: Take only one course
  4. Sub-Sentence Head: Course
  5. 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);

insert image description here


  1. Find the student number, name and course number of students who have taken at least one course that is the same as 'Lisi';
  1. Center language: student number, name and course number of the student
  2. The two tables are merged according to the index Student.sno=SC.sno
  3. Modifier: Take at least one elective course same as 'Lisi'
  4. Sub-Sentence Head:Students of the same course
  5. 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));

insert image description here


  1. Basic information of students who take at least 'C language' or 'Database' courses;
  • Include only one or both of:
  1. Center language: basic information of students
  2. The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
  3. Modifier: Take at least 'C language' or 'Database' courses
  4. Sub-Sentence Head: Course
  5. 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);

insert image description here

  • Both courses include:
  1. Center language: basic information of students
  2. The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
  3. Modifier: Take at least 'C language' or 'Database' courses
  4. Sub-Sentence Head: Course
  5. 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);

insert image description here


  1. List the details of all courses taken, including course number, course name, student number, name and grades
  1. Center language: course number, course name, student number, name and grade
  2. The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
  3. 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;

insert image description here


  1. Query the course number and course name of a course that is only taken by one student
  1. Center language: course number, course name
  2. The two tables are merged according to the index Course.cno=SC.cno
  3. 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;

insert image description here


11. Use a nested query to list the student numbers and names of students who have taken the course 'Data structure'

  1. Center language: student number and name
  2. The three tables are merged according to the index Student.sno=SC.sno and Course.cno=SC.cno
  3. Modifier: 'Data structure' course taken
  4. Sub-Sentence Head: Course
  5. 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');

insert image description here


  1. Use a nested query to query the student name, age, and department of a student younger than AI in another department;
  1. Head: student name, age, and department
  2. Select from table Student
  3. Modifier 1: other departments
  4. 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';

insert image description here

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

Guess you like

Origin blog.csdn.net/m0_52427832/article/details/127670404