课程表
– Table structure for course
DROP TABLE IF EXISTS course
;
CREATE TABLE course
(
id
int(11) NOT NULL,
name
varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
teacherid
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
– Records of course
BEGIN;
INSERT INTO course
VALUES (105, ‘计算机导论’, 825);
INSERT INTO course
VALUES (166, ‘数据电路’, 856);
INSERT INTO course
VALUES (245, ‘操作系统’, 804);
INSERT INTO course
VALUES (888, ‘高等数学’, 100);
COMMIT;
教师表
– DROP TABLE IF EXISTS teacher
;
– CREATE TABLE teacher
(
– id
int(11) NOT NULL,
– name
varchar(255) CHARACTER SET utf8 DEFAULT NULL,
– gender
varchar(255) CHARACTER SET utf8 DEFAULT NULL,
– birthday
date DEFAULT NULL,
– title
varchar(255) CHARACTER SET utf8 DEFAULT NULL,
– department
varchar(255) CHARACTER SET utf8 DEFAULT NULL,
– PRIMARY KEY (id
)
– ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
– Records of teacher
– BEGIN;
– INSERT INTO teacher
VALUES (804, ‘毛涤尘’, ‘男’, ‘1973-12-02’, ‘副教授’, ‘计算机系’);
– INSERT INTO teacher
VALUES (825, ‘向小芳’, ‘女’, ‘1988-05-05’, ‘讲师’, ‘计算机系’);
– INSERT INTO teacher
VALUES (831, ‘刘冰’, ‘女’, ‘1977-08-14’, ‘助教’, ‘电子工程系’);
– INSERT INTO teacher
VALUES (856, ‘张旭’, ‘男’, ‘1985-03-12’, ‘讲师’, ‘电子工程系’);
– COMMIT;
学生表
– DROP TABLE IF EXISTS student
;
– CREATE TABLE student
(
– id
int(11) NOT NULL AUTO_INCREMENT,
– name
varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
– gender
varchar(4) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
– birthday
date DEFAULT NULL,
– class
int(11) DEFAULT NULL,
– PRIMARY KEY (id
)
– ) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=latin1;
– Records of student
– BEGIN;
– INSERT INTO student
VALUES (101, ‘梁前磊’, ‘男’, ‘1992-02-20’, 95033);
– INSERT INTO student
VALUES (103, ‘陈盛杰’, ‘男’, ‘1995-06-03’, 95031);
– INSERT INTO student
VALUES (105, ‘黄振’, ‘男’, ‘2000-10-02’, 95031);
– INSERT INTO student
VALUES (107, ‘徐金峰’, ‘男’, ‘1991-01-23’, 95033);
– INSERT INTO student
VALUES (108, ‘罗昌’, ‘男’, ‘1995-09-01’, 95033);
– INSERT INTO student
VALUES (109, ‘陆岩明’, ‘女’, ‘1994-02-10’, 95031);
– COMMIT;
分数表
– Table structure for score
– DROP TABLE IF EXISTS score
;
– CREATE TABLE score
(
– studentid
int(11) DEFAULT NULL,
– courseid
int(11) DEFAULT NULL,
– grade
varchar(255) DEFAULT NULL
– ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
– Records of score
– BEGIN;
– INSERT INTO score
VALUES (103, 245, ‘86’);
– INSERT INTO score
VALUES (105, 245, ‘75’);
– INSERT INTO score
VALUES (109, 245, ‘68’);
– INSERT INTO score
VALUES (103, 105, ‘92’);
– INSERT INTO score
VALUES (105, 105, ‘88’);
– INSERT INTO score
VALUES (109, 105, ‘76’);
– INSERT INTO score
VALUES (101, 105, ‘64’);
– INSERT INTO score
VALUES (107, 105, ‘91’);
– INSERT INTO score
VALUES (108, 105, ‘78’);
– INSERT INTO score
VALUES (101, 166, ‘85’);
– INSERT INTO score
VALUES (107, 106, ‘79’);
– INSERT INTO score
VALUES (108, 166, ‘81’);
– COMMIT;
查询案例
– 1、 查询Student表中的所有记录的name、gender和Class列。
– select name ,gender, class from student;
– 2、查询Student表中不姓“王”的同学记录。
– select * from student where name not like ‘王%’;
– %可以匹配任意个任意字符. _ 自能配置一个
– 3、 查询Student表的所有记录。
– select * from student;
– 4、 查询Score表中成绩在60到80之间的所有记录。
– select * from score where grade between 60 and 80;
– select * from score where grade >= 60 and grade <= 80;
– 5、 查询Score表中成绩为83,82或86的记录。
– select * from score where grade in ( 83,82,86);
– 6、 查询Student表中“95031”班或性别为“女”的同学记录。
– select * from
– student
– where
– class = 95031 or gender = ‘女’;
– 7、 以Class降序查询Student表的所有记录。
– select * from student ORDER BY class desc;
– 8、 以courseid升序、grade降序查询Score表的所有记录。
– select * from score ORDER BY courseid asc , grade desc;
– 9、 查询“95031”班的学生人数。
– select count(*) from student where class = 95031
– 10、查询Score表中的最高分的学生学号和课程号。
– select * from score where grade = (select max(grade)from score);
– 11、查询105号课程的平均分。
– select avg(grade) from score where courseid = 105;
– 12、查询Score表中至少有5名学生选修的并以1开头的课程的平均分数。
– select * from score where courseid like ‘2%’ and (select count(*) from score where courseid like ‘2%’) > 2
– 13、查询每门成绩都在60-85分之间的studentid列。
– select studentid from score where grade BETWEEN 60 and 85;
– 14、查询所有学生的name、courseid和grade列。(两表联查)
– select st.name , s.courseid, s.grade from student st , score s where st.id=s.courseid;
– 15、查询所有学生的id、课程name和grade列。
– select s.id , c.name,ss.grade
– from
– student s,course c,score ss
– where
– s.id= ss.studentid
– and
– c.id = ss.courseid
– 16、查询所有学生的name、课程name和grade列。(有重名的列)
– select s.name , c.name as aa, g.grade
– from
– student s,course c,score g
– where
– s.id = g.studentid
– and
– c.id = g.courseid
– 17、查询“95033”班所选课程的平均分。select 和having的执行顺序
– select s.courseid ,avg(s.grade)
– from
– score s
– inner join student stu
– on stu.id = s.studentid
– where stu.class = 95033
– GROUP BY s.courseid
– SELECT ss.courseid ,avg(ss.grade)
– from score ss,student stu
– where stu.id=ss.studentid and stu.class = 95033
– GROUP BY ss.courseid
– 18、假设使用如下命令建立了一个gradeRank表:有最低分数,最高分数,等级
– create table gradeRank (low int(3),upp int(3),rank char(1));
– insert into gradeRank values(90,100, ‘A’);
– insert into gradeRank values(80,89, ‘B’);
– insert into gradeRank values(70,79, ‘C’);
– insert into gradeRank values(60,69, ‘D’);
– insert into gradeRank values(0,59, ‘E’);
– 现查询所有同学的id、课程id和rank列。(假设学生的分数是85分,那么他的rank就是B)
– select s.Sno,sc.Cno,sc.Degree,
– CASE
– WHEN sc.Degree>=90 and sc.Degree<=100 THEN ‘A’
– WHEN sc.Degree>=80 AND sc.Degree<=89 THEN ‘B’
– WHEN sc.Degree>=70 AND sc.Degree<=79 THEN ‘C’
– WHEN sc.Degree <=60 and sc.Degree<=69 THEN ‘D’
– ELSE ‘E’
– END as “rank”
– from student s
– inner join score sc
– on sc.Sno =s.sno
– 19、查询选修105课程的成绩高于109号同学成绩的所有同学的记录。
– select stu.* from
– student stu ,score s
– where
– stu.id = s.studentid and s.courseid =105
– and s.grade > (select grade from score where studentid = 109 and courseid =105)
– 20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
– select * from
– score a
– where
– a.studentid in(select studentid from score GROUP BY studentid HAVING count(*) > 1)
– and
– a.grade < (select max(grade) from score b where a.studentid=b.studentid)
– 21、查询成绩高于学号为“109”、课程号为105的成绩的所有记录。
– select stu.,s. from
– student stu,score s
– where
– stu.id = s.studentid
– and s.grade > (select grade from score where studentid=109 and courseid=105)
– 22、查询和学号为108学生同年出生的id、name和birthday列。
– select id,name,birthday from student
– where
– YEAR(birthday) = (select YEAR(birthday) from student where id = 108)
– and
– id != 108;
– 23、查询“向小芳“教师任课的学生成绩。
– SELECT * FROM score WHERE courseid = (
– select id from course
– WHERE
– teacherid = (
– SELECT id
– from teacher where name
= ‘向小芳’))
– 24、查询选修某课程的同学人数多于5人的教师姓名。
– SELECT t.name
FROM teacher t, course c ,score s WHERE t.id=c.teacherid
– and
– c.id = s.courseid
– and
– s.courseid in (
– SELECT courseid FROM score GROUP BY courseid HAVING COUNT(courseid) > 2)
– GROUP BY t.name
– 25、查询95033班和95031班全体学生的信息。
– SELECT * FROM student WHERE class=95033 or class = 95031
– 26、查询存在有85分以上成绩的课程id. ( 查询所有成绩都大于85分的课程id)
– SELECT courseid FROM score where grade > 85;
– 27、查询出“计算机系“教师所教课程的成绩表。–105
– SELECT * FROM score
– WHERE
– courseid
– in(
– SELECT id FROM course WHERE teacherid in (SELECT id FROM teacher where department = ‘计算机系’))
– 28、查询“计算机系”与“电子工程系“不同职称的教师的name和title。(我系有,你没有,你有,我没有)
– SELECT name ,title FROM teacher where department=‘计算机系’ and title not in (SELECT title FROM teacher WHERE department=‘电子工程系’)
– UNION
– SELECT name ,title FROM teacher where department=‘电子工程系’ and title not in (SELECT title FROM teacher WHERE department=‘计算机系’)
– 29、查询选修编号为105课程且成绩至少高于85同学的id、课程id和grade,并按grade从高到低次序排序。
– SELECT * FROM score WHERE courseid = 105 AND grade > 85 ORDER BY grade desc
– 30、查询所有教师和同学的name、sex和birthday.
– SELECT name,gender,birthday FROM student
– UNION
– SELECT name,gender,birthday FROM teacher
– 31、查询所有“女”教师和“女”同学的name、sex和birthday.
– SELECT name,gender,birthday FROM student WHERE gender=‘女’
– UNION
– SELECT name,gender,birthday FROM teacher WHERE gender=‘女’
– 32、查询成绩比该课程平均成绩低的同学的成绩表。
– SELECT * FROM score ss WHERE grade < (
– SELECT AVG(grade) FROM score s WHERE ss.courseid=s.courseid) ORDER BY grade DESC
– SELECT a.* FROM
– score a,(SELECT AVG(grade) as grades , courseid FROM score GROUP BY courseid) b
– WHERE
– a.courseid = b.courseid
– and a.grade < b.grades
– ORDER BY a.grade DESC
– 33、查询所有任课教师的name和Department.
– SELECT t.name
,t.department FROM course c ,teacher t
– where c.teacherid=t.id
– 34 查询所有未讲课的教师的name和Department.
– SELECT * FROM teacher WHERE id not in (SELECT teacherid FROM course )
– 35、查询至少有2名男生的班号。
– SELECT class FROM student WHERE gender=‘男’ GROUP BY class HAVING count(gender) > 1
– 36、查询“男”教师及其所上的课程。
– SELECT t.* FROM teacher t,course c WHERE t.gender=‘男’ and t.id = c.teacherid
– 37、 查询教师所有的单位即不重复的department列。
– SELECT DISTINCT department FROM teacher
– 38、查询Student表中最大和最小的birthday日期值。
– SELECT MAX(birthday),MIN(birthday) FROM student
– 39、查询所有选修“计算机导论”课程的“男”同学的成绩表
– SELECT s.grade
– FROM
– score s, student stu,course c
– WHERE
– stu.id = s.studentid
– and
– c.id = s.courseid
– and
– stu.gender=‘男’
– and
– c.name
=‘计算机导论’
– 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。(注意两个排序的前后顺序)
– SELECT * FROM student ORDER BY class desc,birthday desc
– 41 查询和“梁前磊”同性别并同班的同学姓名.
SELECT name
FROM student
WHERE
gender=(SELECT gender FROM student WHERE name
=‘梁前磊’)
and
class=(SELECT class FROM student WHERE name
=‘梁前磊’)
and
name != ‘梁前磊’