create table class ( cid int(11) primary key auto_increment, caption varchar(32) not null ); insert into class(caption) values("三年二班"),("三年三班"),("一年二班"),("二年九班"); select * from class; +-----+--------------+ | cid | caption | +-----+--------------+ | 1 | 三年二班 | | 2 | 三年三班 | | 3 | 一年二班 | | 4 | 二年九班 | +-----+--------------+
create table student( sid int(11) primary key auto_increment, gender char(1) not null, class_id int(11) not null, sname varchar(32) not null, foreign key(class_id) references class(cid) ); insert into student values('1', '男', '1', '理解'), ('2', '女', '1', '鋼蛋'), ('3', '男', '1', '張三'), ('4', '男', '1', '張一'), ('5', '女', '1', '張二'), ('6', '男', '1', '張四'), ('7', '女', '2', '鐵錘'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '劉三'), ('14', '男', '3', '劉一'), ('15', '女', '3', '劉二'), ('16', '男', '3', '劉四'); select * from student; +-----+--------+----------+--------+ | sid | gender | class_id | sname | +-----+--------+----------+--------+ | 1 | 男 | 1 | 理解 | | 2 | 女 | 1 | 鋼蛋 | | 3 | 男 | 1 | 張三 | | 4 | 男 | 1 | 張一 | | 5 | 女 | 1 | 張二 | | 6 | 男 | 1 | 張四 | | 7 | 女 | 2 | 鐵錘 | | 8 | 男 | 2 | 李三 | | 9 | 男 | 2 | 李一 | | 10 | 女 | 2 | 李二 | | 11 | 男 | 2 | 李四 | | 12 | 女 | 3 | 如花 | | 13 | 男 | 3 | 劉三 | | 14 | 男 | 3 | 劉一 | | 15 | 女 | 3 | 劉二 | | 16 | 男 | 3 | 劉四 | +-----+--------+----------+--------+
create table teacher( tid int(11) primary key auto_increment, tname varchar(32) not null ); insert into teacher values('1', '張磊老師'), ('2', '李平老師'), ('3', '劉海燕老師'), ('4', '朱雲海老師'), ('5', '李傑老師'); select * from teacher; +-----+-----------------+ | tid | tname | +-----+-----------------+ | 1 | 張磊老師 | | 2 | 李平老師 | | 3 | 劉海燕老師 | | 4 | 朱雲海老師 | | 5 | 李傑老師 | +-----+-----------------+
create table course( cid int(11) primary key auto_increment, cname varchar(32) not null, teacher_id int(11) not null, foreign key(teacher_id) references teacher(tid) ); insert into course values('1', '生物', '1'), ('2', '物理', '2'), ('3', '體育', '3'), ('4', '美術', '2'); select * from course; +-----+--------+------------+ | cid | cname | teacher_id | +-----+--------+------------+ | 1 | 生物 | 1 | | 2 | 物理 | 2 | | 3 | 體育 | 3 | | 4 | 美術 | 2 | +-----+--------+------------+
create table score( sid int(11) primary key auto_increment, student_id int(11) not null, course_id int(11) not null, num int(11) not null, foreign key(student_id) references student(sid), foreign key(course_id) references course(cid) ); insert into score values('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87'); select * from score; +-----+------------+-----------+-----+ | sid | student_id | course_id | num | +-----+------------+-----------+-----+ | 1 | 1 | 1 | 10 | | 2 | 1 | 2 | 9 | | 5 | 1 | 4 | 66 | | 6 | 2 | 1 | 8 | | 8 | 2 | 3 | 68 | | 9 | 2 | 4 | 99 | | 10 | 3 | 1 | 77 | | 11 | 3 | 2 | 66 | | 12 | 3 | 3 | 87 | | 13 | 3 | 4 | 99 | | 14 | 4 | 1 | 79 | | 15 | 4 | 2 | 11 | | 16 | 4 | 3 | 67 | | 17 | 4 | 4 | 100 | | 18 | 5 | 1 | 79 | | 19 | 5 | 2 | 11 | | 20 | 5 | 3 | 67 | | 21 | 5 | 4 | 100 | | 22 | 6 | 1 | 9 | | 23 | 6 | 2 | 100 | | 24 | 6 | 3 | 67 | | 25 | 6 | 4 | 100 | | 26 | 7 | 1 | 9 | | 27 | 7 | 2 | 100 | | 28 | 7 | 3 | 67 | | 29 | 7 | 4 | 88 | | 30 | 8 | 1 | 9 | | 31 | 8 | 2 | 100 | | 32 | 8 | 3 | 67 | | 33 | 8 | 4 | 88 | | 34 | 9 | 1 | 91 | | 35 | 9 | 2 | 88 | | 36 | 9 | 3 | 67 | | 37 | 9 | 4 | 22 | | 38 | 10 | 1 | 90 | | 39 | 10 | 2 | 77 | | 40 | 10 | 3 | 43 | | 41 | 10 | 4 | 87 | | 42 | 11 | 1 | 90 | | 43 | 11 | 2 | 77 | | 44 | 11 | 3 | 43 | | 45 | 11 | 4 | 87 | | 46 | 12 | 1 | 90 | | 47 | 12 | 2 | 77 | | 48 | 12 | 3 | 43 | | 49 | 12 | 4 | 87 | | 52 | 13 | 3 | 87 | +-----+------------+-----------+-----+
查詢全部的課程的名稱以及對應的任課老師姓名mysql
mysql> select cid,cname,tname from course left join teacher on teacher_id = tid order by cid; +-----+--------+-----------------+ | cid | cname | tname | +-----+--------+-----------------+ | 1 | 生物 | 張磊老師 | | 2 | 物理 | 李平老師 | | 3 | 體育 | 劉海燕老師 | | 4 | 美術 | 李平老師 | +-----+--------+-----------------+
查詢學生表中男女生各有多少人sql
mysql> select gender,count(*) as total from student group by gender; +--------+-------+ | gender | total | +--------+-------+ | 女 | 6 | | 男 | 10 | +--------+-------+
查詢物理成績等於100的學生的姓名code
select sname from student join score join course on student.sid = student_id and course_id = course.cid where cname = "物理" and num = 100; +--------+ | sname | +--------+ | 張四 | | 鐵錘 | | 李三 | +--------+
查詢平均成績大於八十分的同窗的姓名和平均成績排序
select sname,sum(num)/4 average_score from student join score on student_id = student.sid group by sname having sum(num)/4 > 80; +--------+---------------+ | sname | average_score | +--------+---------------+ | 張三 | 82.2500 | +--------+---------------+
查詢全部學生的學號,姓名,選課數,總成績ci
select student.sid as sid,sname,count(*) count_course,sum(num) summary from student left join score on student_id = student.sid group by sname order by student.sid; +-----+--------+--------------+---------+ | sid | sname | count_course | summary | +-----+--------+--------------+---------+ | 1 | 理解 | 3 | 85 | | 2 | 鋼蛋 | 3 | 175 | | 3 | 張三 | 4 | 329 | | 4 | 張一 | 4 | 257 | | 5 | 張二 | 4 | 257 | | 6 | 張四 | 4 | 276 | | 7 | 鐵錘 | 4 | 264 | | 8 | 李三 | 4 | 264 | | 9 | 李一 | 4 | 268 | | 10 | 李二 | 4 | 297 | | 11 | 李四 | 4 | 297 | | 12 | 如花 | 4 | 297 | | 13 | 劉三 | 1 | 87 | | 14 | 劉一 | 1 | NULL | | 15 | 劉二 | 1 | NULL | | 16 | 劉四 | 1 | NULL | +-----+--------+--------------+---------+
查詢姓李老師的個數rem
select group_concat(tname) tname,count(*) count from teacher where tname like "李%"; +---------------------------+-------+ | tname | count | +---------------------------+-------+ | 李平老師,李傑老師 | 2 | +---------------------------+-------+
查詢沒有報李平老師課的學生姓名it
select sname from student where sid not in ( select student_id from score where course_id in ( select cid from course where teacher_id in ( select tid from teacher where tname = "李平老師" ) ) ); +--------+ | sname | +--------+ | 劉三 | | 劉一 | | 劉二 | | 劉四 | +--------+
select t1.student_id from( (select * from score where course_id in (select cid from course where cname = '生物')) t1 left join (select * from score where course_id in (select cid from course where cname = '物理')) t2 on t1.student_id = t2.student_id) where t1.num < t2.num; +------------+ | student_id | +------------+ | 6 | | 7 | | 8 | +------------+
select sname from student where sid not in ( select student_id from score where course_id in ( select cid from course where cname in ("物理","體育") ) group by student_id having count(*) = 2 ); +--------+ | sname | +--------+ | 理解 | | 鋼蛋 | | 劉三 | | 劉一 | | 劉二 | | 劉四 | +--------+
select sname,caption from student join class on cid = class_id where sid=(select student_id from score where num < 60 group by student_id having count(*) >= 2); +--------+--------------+ | sname | caption | +--------+--------------+ | 理解 | 三年二班 | +--------+--------------+
查詢選修了全部課程的學生姓名io
select sname from student where sid in ( select student_id from score group by student_id having count(*) = 4 ); +--------+ | sname | +--------+ | 張三 | | 張一 | | 張二 | | 張四 | | 鐵錘 | | 李三 | | 李一 | | 李二 | | 李四 | | 如花 | +--------+
查詢李平老師教的課程的全部成績記錄table
select num from score where course_id in ( select cid from course where teacher_id = ( select tid from teacher where tname = "李平老師" ) ); +-----+ | num | +-----+ | 9 | | 66 | | 11 | | 11 | | 100 | | 100 | | 100 | | 88 | | 77 | | 77 | | 77 | | 66 | | 99 | | 99 | | 100 | | 100 | | 100 | | 88 | | 88 | | 22 | | 87 | | 87 | | 87 | +-----+
查詢所有學生都選修了的課程號和課程名class
select cid,cname from course where cid in( select course_id from score group by course_id having count(*) = 4 ); Empty set (0.00 sec)
查詢每門課程被選修的次數
select cname,count(*) from course join score on cid = course_id group by course_id; +--------+----------+ | cname | count(*) | +--------+----------+ | 生物 | 12 | | 物理 | 11 | | 體育 | 12 | | 美術 | 12 | +--------+----------+
查詢之選修了一門課程的學生姓名和學號
select sname,sid from student where sid in( select student_id from score group by student_id having count(*) = 1 ); +--------+-----+ | sname | sid | +--------+-----+ | 劉三 | 13 | +--------+-----+
查詢全部學生考出的成績並按從高到低排序(成績去重)
select distinct num from score order by num desc; +-----+ | num | +-----+ | 100 | | 99 | | 91 | | 90 | | 88 | | 87 | | 79 | | 77 | | 68 | | 67 | | 66 | | 43 | | 22 | | 11 | | 10 | | 9 | | 8 | +-----+
查詢平均成績大於85的學生姓名和平均成績
select sname,avg(num) average_score from student join score on student_id = student.sid group by sname having avg(num) > 85; +--------+---------------+ | sname | average_score | +--------+---------------+ | 劉三 | 87.0000 | +--------+---------------+
查詢生物成績不及格的學生姓名和對應生物分數
select sname,num from student join score on student.sid = student_id where num < 60 and course_id = ( select cid from course where cname = "生物" ); +--------+-----+ | sname | num | +--------+-----+ | 理解 | 10 | | 鋼蛋 | 8 | | 張四 | 9 | | 鐵錘 | 9 | | 李三 | 9 | +--------+-----+
查詢在全部選修了李平老師課程的學生中,這些課程(李平老師的課程,不是全部課程)平均成績最高的學生姓名
select sname,avg(num) from student join score join course join teacher on student.sid = student_id and course.cid = course_id and teacher_id = tid where tname = "李平老師" group by student_id order by avg(num) desc limit 1; +--------+----------+ | sname | avg(num) | +--------+----------+ | 張四 | 100.0000 | +--------+----------+
查詢每門課程成績最好的前兩名學生姓名
(select cname,sname from course join score join student on course_id = course.cid and student.sid = student_id where cname = "生物" order by num desc limit 2) union (select cname,sname from course join score join student on course_id = course.cid and student.sid = student_id where cname = "物理" order by num desc limit 2) union (select cname,sname from course join score join student on course_id = course.cid and student.sid = student_id where cname = "美術" order by num desc limit 2) union (select cname,sname from course join score join student on course_id = course.cid and student.sid = student_id where cname = "體育" order by num desc limit 2) +--------+--------+ | cname | sname | +--------+--------+ | 生物 | 李一 | | 生物 | 如花 | | 物理 | 李三 | | 物理 | 鐵錘 | | 美術 | 張四 | | 美術 | 張二 | | 體育 | 劉三 | | 體育 | 張三 | +--------+--------+
查詢不一樣課程但成績相同的學號,課程號,成績
select distinct s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id; +-----------+-----------+-----+-----+ | course_id | course_id | num | num | +-----------+-----------+-----+-----+ | 1 | 2 | 9 | 9 | | 2 | 4 | 66 | 66 | | 2 | 1 | 77 | 77 | | 4 | 2 | 66 | 66 | | 4 | 3 | 87 | 87 | | 2 | 4 | 100 | 100 | | 2 | 1 | 9 | 9 | | 4 | 2 | 100 | 100 | | 2 | 4 | 88 | 88 | | 4 | 2 | 88 | 88 | | 1 | 2 | 77 | 77 | | 3 | 4 | 87 | 87 | +-----------+-----------+-----+-----+
查詢沒學過「葉平」老師課程的學生姓名以及選修的課程名稱;
select sname,cname from student join score join course on student.sid = student_id and course.cid = course_id where student.sid not in ( select distinct student_id from score where course_id in( select cid from course where teacher_id in ( select tid from teacher where tname = "李平老師" ) ) ); +--------+--------+ | sname | cname | +--------+--------+ | 劉三 | 體育 | +--------+--------+
查詢全部選修了學號爲1的同窗選修過的一門或者多門課程的同窗學號和姓名;
select sid,sname from student where sid in ( select student_id from score where course_id in( select course_id from score where student_id = 1 ) ); +-----+--------+ | sid | sname | +-----+--------+ | 1 | 理解 | | 2 | 鋼蛋 | | 3 | 張三 | | 4 | 張一 | | 5 | 張二 | | 6 | 張四 | | 7 | 鐵錘 | | 8 | 李三 | | 9 | 李一 | | 10 | 李二 | | 11 | 李四 | | 12 | 如花 | +-----+--------+
任課最多的老師中學生單科成績最高的學生姓名
select sname from student join score join course on student_id = student.sid and course_id = cid where teacher_id = ( select teacher_id from (select teacher_id,count(*) as total from course group by teacher_id order by total desc limit 1) as t ) group by course_id having max(num);