先给出需求,然后每天自己写一点出来当作练习和巩固,每天都会更新;坚持,加油!
一、原始数据
create database practice;
USE PRACTICE;
CREATE TABLE STUDENT
(SNO VARCHAR(3) NOT NULL,
SNAME VARCHAR(4) NOT NULL,
SSEX VARCHAR(2) NOT NULL,
SBIRTHDAY DATE,
CLASS VARCHAR(5));
CREATE TABLE COURSE
(CNO VARCHAR(5) NOT NULL,
CNAME VARCHAR(10) NOT NULL,
TNO VARCHAR(10) NOT NULL);
CREATE TABLE SCORE
(SNO VARCHAR(3) NOT NULL,
CNO VARCHAR(5) NOT NULL,
DEGREE NUMERIC(10, 1) NOT NULL) ;
CREATE TABLE TEACHER
(TNO VARCHAR(3) NOT NULL,
TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL,
TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6),
DEPART VARCHAR(10) NOT NULL);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108,'曾华'
,'男' ,'1977-09-01',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105,'匡明'
,'男' ,'1975-10-02',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107,'王丽'
,'女' ,'1976-01-23',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101,'李军'
,'男' ,'1976-02-20',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109,'王芳'
,'女' ,'1975-02-10',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103,'陆君'
,'男' ,'1974-06-03',95031);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERTINTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERTINTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERTINTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERTINTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERTINTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');
select*from STUDENT;
select*from COURSE;
select*from SCORE;
select*from TEACHER;
select SNAME,SSEX,CLASS from STUDENT;
2、 查询教师所有的单位即不重复的Depart列。
select distinct DEPART from TEACHER;
3、 查询Student表的所有记录。
select * from STUDENT;
4、 查询Score表中成绩在60到80之间的所有记录。
select * from SCORE where DEGREE between 60 and 80;
5、 查询Score表中成绩为85,86或88的记录。
select * from SCORE where DEGREE in (85,86,88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from STUDENT where CLASS='95031' or SSEX='女';
7、 以Class降序查询Student表的所有记录。
select * from STUDENT order by CLASS desc;
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from SCORE order by CNO asc,DEGREE desc;
9、 查询“95031”班的学生人数。
select count(*) from STUDENT where CLASS='95031';
10、查询Score表中的最高分的学生学号和课程号。
select SNO,CNO from SCORE where DEGREE=(select max(DEGREE) from SCORE);
select SNO,CNO from SCORE order by DEGREE desc limit 1;
select avg(DEGREE) from SCORE where CNO='3-105';
12、
查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(DEGREE),cno
from score
where cno like '3%'
group by cno
having count(sno)>= 5;
13、查询最低分大于70,最高分小于90的Sno列。
select SNO from SCORE groupby SNO having min(DEGREE)>70 and max(DEGREE) <90;
14、查询所有学生的Sname、Cno和Degree列。
select A.SNAME,B.DEGREE from STUDENT as A join SCORE as B on A.CNO=B.CNO ;
15、查询所有学生的Sno、Cname和Degree列。
select Sno, Cname , Degree from (select * from DEGREE left join COURSE on CNO) ;
select A.CNAME,B.SNO, B.DEGREE from COURSE as A join SCORE as B on A.CNO=B.CNO;
select A.SNAME ,B.CNAME, C.DEGREE from STUDENT A join (COURSE B,SCORE C) on A.SNO=C.SNO and B.CNO =C.CNO ;
17、查询“95033”班所选课程的平均分。
select avg(A.DEGREE ) from SCORE A join STUDENT B on A.SNO =B.SNO where B.CLASS='95033';
select A.SNO ,A.CNO,B.RANK from SCORE A, GRADE B where A.DEGREE between B.LOW and B.UPP order by RANK;
19、
查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select A.* from STUDENT A join SCORE B where B.CNO='3-105' and A.DEGREE >B.DEGREE and B.SNO ='109' AND B.CNO='3-105' ;
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select * from SCORE where DEGREE <( select max(DEGREE ) from SCORE) group by SNO having count(SNO) >1 order by DEGREE;
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select SNO, SNAME ,SBIRTHDAY from STUDENT where year (SBIRTHDAY)=(select year(SBIRTHDAY) from STUDENT where SNO='108') ;
23、查询“张旭“教师任课的学生成绩。
select A.SNO,A.DEGREE , from SCORE A join (TEACHER B ,COURSE C ) on A.CNO=C.CNO and B.TNO=C.TNO where B . TNAME= '张旭' ;
24、查询选修某课程的同学人数多于5人的教师姓名。
select A.TNAME from TEACHER A join (COURSE B , SCORE C) on (A.TNO =B.TNO and B.CNO=C.CNO)
group by C.CNO having count (C.CNO)>5;