码迷,mamicode.com
首页 > 数据库 > 详细

Mysql查询处理练习

时间:2018-10-10 16:10:34      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:into   date   电子   假设   sel   select   ble   高等数学   mit   

表的创建:
CREATE
TABLE STUDENT (SNO VARCHAR(3) NOT NULL, SNAME VARCHAR(4) NOT NULL, SSEX VARCHAR(2) NOT NULL, SBIRTHDAY DATETIME, 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); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,3-245,86); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,3-245,75); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,3-245,68); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,3-105,92); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,3-105,88); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,3-105,76); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,3-105,64); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,3-105,91); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,3-105,78); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,6-166,85); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,6-106,79); INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,6-166,81); INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,李诚,,1958-12-02,副教授,计算机系); INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,张旭,,1969-03-12,讲师,电子工程系); INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,王萍,,1972-05-05,助教,计算机系); INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,刘冰,,1977-08-14,助教,电子工程系);

表的查询:(这里的联系会包含伪表的应用,基础查询基本可以表示结束了)
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
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 in(95031) OR ssex in(‘女‘);
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 class,COUNT(sno) FROM student WHERE class="95031";
10、查询Score表中的最高分的学生学号和课程号。
SELECT sno,cno FROM score where degree in(SELECT MAX(degree) FROM score);
11、查询‘3-105’号课程的平均分。
SELECT AVG( degree) FROM score WHERE cno=‘3-105‘;
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT cno,AVG(degree) FROM score WHERE cno like"3%" AND cno IN(SELECT cno FROM score GROUP BY cno HAVING COUNT(cno)>5)  ; 
SELECT cno,AVG(DEGREE) FROM score GROUP BY cno HAVING COUNT(cno)>5 AND cno like"3%";
13、查询最低分大于70,最高分小于90的Sno列。
SELECT sno,degree FROM score where degree BETWEEN 70 AND 90;
14、查询所有学生的Sname、Cno和Degree列。
SELECT sname,cno,degree FROM student s JOIN score sc ON sc.sno=s.sno;
15、查询所有学生的Sno、Cname和Degree列。
SELECT sno,cname,degree FROM course c JOIN score sc ON c.cno=sc.cno;
16、查询所有学生的Sname、Cname和Degree列。
SELECT sname,cname,degree FROM student s JOIN score sc ON sc.sno=s.sno JOIN course c ON c.cno=sc.cno;
17、查询“95033”班所选课程的平均分。
SELECT AVG(degree) FROM student s JOIN score sc ON sc.sno=s.sno WHERE class="95033";
18、假设使用如下命令建立了一个grade表:
create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100, ‘A‘);
insert into grade values(80,89, ‘B‘);
insert into grade values(70,79, ‘C‘);
insert into grade values(60,69, ‘D‘);
insert into grade values(0,59, ‘E‘);
commit;
现查询所有同学的Sno、Cno和rank列。
SELECT sno,cno,rank FROM score sc JOIN grade WHERE degree BETWEEN low AND upp;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT * FROM score WHERE cno=‘3-105‘ AND degree>(SELECT degree FROM score WHERE cno=‘3-105‘ AND sno=109);

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select *  from score t2,(select sno,max(degree)   from score group by sno having count(sno)>1 )as t1   where t1.sno=t2.sno and t2.degree<t1.degree;
SELECT sno,degree FROM score

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SELECT * FROM score WHERE sno=109 AND cno=‘3-105‘;
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday)=(SELECT YEAR(sbirthday) FROM student WHERE sno=108);
23、查询“张旭“教师任课的学生成绩。
SELECT * FROM score sc JOIN course c ON sc.cno=c.cno JOIN teacher t ON t.tno=c.tno WHERE TNAME=‘张旭‘;
24、查询选修某课程的同学人数多于5人的教师姓名。
SELECT t.tname FROM score sc JOIN course c ON sc.cno=c.cno JOIN teacher t ON t.tno=c.tno GROUP BY c.cno HAVING COUNT(sno)>5;
25、查询95033班和95031班全体学生的记录。
SELECT * FROM student WHERE class="95033" OR class="95031";
26、查询存在有85分以上成绩的课程Cno
SELECT cno FROM score WHERE degree>85;
27、查询出“计算机系“教师所教课程的成绩表。
SELECT * FROM score sc JOIN course c ON sc.cno=c.cno JOIN teacher t ON t.tno=c.tno WHERE t.depart=‘计算机系‘;
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
SELECT t.tname,t.prof,t.depart FROM score sc JOIN course c ON sc.cno=c.cno JOIN teacher t ON t.tno=c.tno WHERE t.depart=‘计算机系‘ OR t.depart=‘电子工程系‘ GROUP BY t.prof;
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
SELECT a.cno,a.sno,a.degree FROM (SELECT * FROM score WHERE cno=‘3-105‘ ) AS a JOIN (SELECT * FROM score WHERE cno=‘3-245‘) AS b ON a.sno=b.sno WHERE a.degree>b.degree ORDER BY a.degree DESC;



 

Mysql查询处理练习

标签:into   date   电子   假设   sel   select   ble   高等数学   mit   

原文地址:https://www.cnblogs.com/CorbieBlack-trip/p/9766848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!