SQL Comprehensive Exercise 1


use test2
create table student(
sno char(9) primary key,
sname char(20) not null,
Sex char(2) not null,
Sage int not null,
Sdept char(20)
)

create table Course(
Cno  char(4)  primary key ,
Cname char(40) not null,
Cpno  char(40) ,
Ccredit  Int not null
)

create table SC(
Sno  char(9) foreign key references  student(sno) not null,
Cno  char(4) foreign key references  Course(cno) not null,
Grade int
)

insert into Student
select '20215121','Li Yong','Male',20,'CS' union
select '20215122','Liu Chen','Male',19,'CS' union
select '20215123','Wang Min','Female',18,'MA' union
select '20215125','Zhang Li','Male',19,'IS'

insert into Course
select '1','Database',NULL,4 union
select '2','Mathematics',NULL,2 union
select '3','Information System',NULL,4 union
select '4','Operation System',NULL,3 union
select '5','data structure',NULL,4 union
select '6','data processing',NULL,2 union
select '7','PASCAL language',NULL,4

insert into SC
select '20215121','1',92 union
select '20215121','2',85 union
select '20215121','3',88 union
select '20215122','2',90 union
select '20215122','3',80 


select * from student
select * from course
select * from sc

--1. Query all information in the student table
select * from student
--2. Query students under the age of 19
select * from student where sage < 19
--3. Query all students' names, exam course numbers, scores
select a.sname 'student name',c.cno 'course number',c.grade 'course score'
from student a
inner join sc c on a.sno=c.sno
--4. Query the course selection of all students , those who did not participate in the course selection need to display
select a.sname 'student name', b.cno 'course number', c.grade 'course score', b.cname 'course name'
from student a
left join sc c on a. sno=c.sno
left join course b on b.cno=c.cno

--5. Query the students who participated in the course selection and the course selection information, and those who did not participate in the course selection will not be displayed
select a.sname 'student name', b.cno 'course number', c.grade 'course score', b.cname ' Course name'
from student a
inner join sc c on a.sno=c.sno
inner join course b on b.cno=c.cno
--6. Query students' grades and display their names and student numbers
select a.sno 'Student number', a.sname 'student name', c.grade 'course score', b.cname 'course name'
from student a
inner join sc c on a.sno=c.sno
inner join course b on b. cno=c.cno
--7. Query the student IDs of students with test scores above 80 (complete this question in 4 different ways)
--Method 1:
select a.sno ​​'student ID', a.* from student a where a.sno ​​in (
select b.sno from sc b where grade>80)
--Method 2:
select distinct(a.sno) 'student number' from student a,sc b where a.sno=b.sno and grade>80
--method 3:
select distinct(a.sno) '学号' from student a
inner join sc b on (a.sno=b.sno) where grade>80
--方法四:
select distinct(a.sno) '学号' from student a
right join sc b on (a.sno=b.sno) where grade>80

--8. Query the names and grades of the students who chose course No. 1, and sort them in descending order of grades
select a.sname 'student name',c.grade 'grade'
from student a,SC c
where a.sno=c. sno and cno =1
order by grade desc
--9. Query the name of the student whose surname is not Liu
select sname 'student name' from student where sname not like '%Liu%'
--10. Query that it is not a computer department (CS) , Department of Mathematics (MA), student name of IS (Department of Information)
select sname 'student name' from student where sdept not in('CS','MA','IS') or sdept is NULL
--11, query out How many people are there in each department
select sum(sno) from student
group by sdept


--12. Query the total credits of the elective course for the student '20215121'
select sum(a.ccredit) 'total credits' from course a
inner join sc c on a.sno=c.sno
where a.sno= '20215121' 


--13. Query the name and score of the student with the highest score in course No. 2
select max(c.grade) 'math score', a.sname 'student name' from student a,course b ,sc c 
where a.sno= c.sno and c.cno=b.cno and c.cno=2
group by a.sno,a.sname,c.grade

select * from student
select * from course
select * from sc

Guess you like

Origin blog.csdn.net/m0_64351096/article/details/128035556