整理45道mysql的经典练习题。
题目:
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');
【要求】
我的答案:
SELECT sname,ssex,class from students; -- #1
SELECT count(*),depart FROM teachers GROUP BY depart; -- #2
SELECT * from students; -- #3
SELECT *FROM scores WHERE degree>60 and degree<80; -- #4
SELECT * FROM scores WHERE degree in(85,86,88); -- #5
SELECT * FROM students WHERE class=95031 || ssex='女'; -- #6
SELECT * FROM students ORDER BY class DESC; -- #7
SELECT * FROM scores ORDER BY cno ASC, degree DESC; -- #8
SELECT COUNT(*) AS '95031班的人数' FROM students WHERE class=95031; -- #9
SELECT sno,cno FROM scores WHERE degree=(SELECT MAX(degree) FROM scores); -- #10
SELECT sno,cno FROM scores ORDER BY degree DESC LIMIT 1; -- #11
SELECT AVG(degree)FROM scores WHERE cno IN(SELECT cno FROM scores GROUP BY cno HAVING COUNT(cno)>5 AND LEFT(cno,1)='3');
-- #12
SELECT AVG(DEgree) FROM scores WHERE cno in(SELECT cno FROM scores WHERE cno LIKE '3%' GROUP BY cno HAVING COUNT(*) >= 5); -- #13
SELECT sno,degree FROM scores WHERE degree>70 AND degree<90; -- #14
SELECT students.sname,scores.cno,scores.degree from students INNER JOIN scores ON students.sno=scores.sno; -- #15
SELECT scores.sno,courses.cname,scores.degree FROM scores INNER JOIN courses ON scores.cno=courses.cno; -- #16
SELECT students.sname,courses.cname,scores.degree FROM students INNER JOIN scores ON students.sno=scores.sno
INNER JOIN courses ON scores.cno=courses.cno; -- #17
SELECT AVG(degree) FROM scores INNER JOIN students ON scores.sno=students.sno WHERE class='95033'; -- #18
SELECT * FROM scores WHERE cno='3-105' AND (SELECT max(degree) FROM scores WHERE sno=109); -- #19
SELECT sno,degree,count(*) as '选课门数' FROM scores WHERE degree<(SELECT MAX(degree) FROM scores) GROUP BY sno HAVING 选课门数>1 ; -- #20
SELECT * FROM scores WHERE degree>(SELECT degree FROM scores WHERE sno=109 and cno='3-105'); -- #21
SELECT sno,sname,sbirthday FROM students WHERE year(sbirthday)=(SELECT YEAR(sbirthday) FROM students WHERE sno=107); -- #22
SELECT degree FROM scores INNER JOIN courses ON scores.cno=courses.cno
INNER JOIN teachers ON teachers.tno=courses.tno WHERE teachers.tname='张旭'; -- #23
SELECT teachers.tname,count(*) AS '同学数量' FROM scores INNER JOIN courses ON scores.cno=courses.cno
INNER JOIN teachers ON teachers.tno=courses.tno GROUP BY teachers.tname HAVING 同学数量>5; -- #24
SELECT * FROM students WHERE class=95033 ||class=95031; -- #25
SELECT cno,degree FROM scores GROUP BY cno HAVING degree>85; -- #26
SELECT cname,degree FROM scores INNER JOIN courses ON scores.cno=courses.cno
INNER JOIN teachers ON courses.tno=teachers.tno WHERE teachers.tname in(SELECT tname FROM teachers WHERE depart='计算机系'); -- #27
SELECT tname,prof FROM teachers WHERE depart in('计算机系','电子工程系') GROUP BY prof; -- #28
SELECT cno,sno,degree FROM scores WHERE cno='3-105'AND degree>(SELECT degree FROM scores
WHERE cno='3-245' ORDER BY degree ASC LIMIT 1) ORDER BY degree DESC; -- #29
SELECT cno,sno,degree FROM scores WHERE cno='3-105'AND degree>(SELECT degree FROM scores WHERE cno='3-245'
ORDER BY degree DESC LIMIT 1) ORDER BY degree DESC; -- #30
SELECT students.sname,students.ssex,students.sbirthday,teachers.tname,teachers.tsex,teachers.tbirthday FROM students
INNER JOIN scores ON students.sno=scores.sno INNER JOIN courses ON scores.cno=courses.cno
INNER JOIN teachers ON courses.tno=teachers.tno; -- #31
SELECT students.sname,students.ssex,students.sbirthday,teachers.tname,teachers.tsex,teachers.tbirthday FROM students
INNER JOIN scores ON students.sno=scores.sno INNER JOIN courses ON scores.cno=courses.cno
INNER JOIN teachers ON courses.tno=teachers.tno WHERE students.ssex='女' AND teachers.tsex='女'; -- #32
SELECT sname,scores.cno,scores.degree,avg_scores.avgdg FROM students INNER JOIN scores ON students.sno=scores.sno
INNER JOIN(SELECT cno,avg(degree) AS avgdg FROM scores GROUP BY cno)AS avg_scores ON scores.cno=avg_scores.cno
WHERE scores.degree<avg_scores.avgdg; -- #33
SELECT tname, depart FROM teachers; -- #34
SELECT tname, depart FROM teachers LEFT JOIN courses ON teachers.tno=courses.tno
LEFT JOIN scores ON courses.cno=scores.cno WHERE courses.tno is NULL; -- #35
select class from students where ssex = '男' group by class having count(ssex) >= 2; -- #36
SELECT * from students WHERE sname NOT LIKE '王%'; -- #37
SELECT sname,year(CURDATE())-year(sbirthday) FROM students; -- #38
SELECT MAX(sbirthday),MIN(sbirthday) FROM students; -- #39
SELECT *,year(CURDATE())-year(sbirthday)AS age FROM students ORDER BY class DESC,age DESC; -- #40
SELECT tname,cname from teachers INNER JOIN courses ON teachers.tno=courses.tno WHERE tsex='男'; -- #41
SELECT sno,cno,degree FROM scores ORDER BY degree DESC LIMIT 1; -- #42
SELECT sname,ssex FROM students WHERE ssex=(SELECT ssex FROM students WHERE sname='李军'); -- #43
SELECT sname,class FROM students WHERE ssex=(SELECT ssex FROM students WHERE sname='李军')
AND class=(SELECT class FROM students WHERE sname='李军'); -- #44
SELECT sname,degree FROM students INNER JOIN scores ON students.sno=scores.sno
INNER JOIN courses ON scores.cno=courses.cno WHERE ssex='男' AND cname='计算机导论'; -- #45