mysql查詢練習(2018.09.27學習筆記)
阿新 • • 發佈:2018-12-12
USE testdb; DROP TABLE Student; DROP TABLE Course; DROP TABLE SC; DROP TABLE Teacher; -- 語法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ] [LIMIT m,n] -- 示例: -- limit 0,10是從第一條開始,取10條資料 SELECT classNo FROM table_name GROUP BY classNo HAVING(AVG(成績)>70) ORDER BY classNo LIMIT 0,10 IFNULL -- Student(S#,Sname,Sage,Ssex) 學生表 CREATE TABLE Student( sid INT PRIMARY KEY, sname VARCHAR(20) NOT NULL DEFAULT '', sage INT, ssex VARCHAR(2) ) -- Course(C#,Cname,T#) 課程表 CREATE TABLE Course( cid INT PRIMARY KEY, cname VARCHAR(20) NOT NULL DEFAULT '', tid INT ) -- Teacher(T#,Tname) 教師表 CREATE TABLE Teacher( tid INT PRIMARY KEY, tname VARCHAR(20) NOT NULL DEFAULT '' ) -- SC(S#,C#,score) 成績表 CREATE TABLE SC( sid INT , cid INT , score INT ) -- 學生表測試資料 INSERT INTO Student VALUES(01 , '趙雷' , 18 , '男'); INSERT INTO Student VALUES(02 , '錢電' , 19 , '男'); INSERT INTO Student VALUES(03 , '孫風' , 22 , '男'); INSERT INTO Student VALUES(04 , '李雲' , 19 , '男'); INSERT INTO Student VALUES(05 , '周梅' , 20 , '女'); INSERT INTO Student VALUES(06 , '吳蘭' , 19 , '女'); INSERT INTO Student VALUES(07 , '鄭竹' , 21 , '女'); INSERT INTO Student VALUES(08 , '王菊' , 18 , '女'); -- 課程表測試資料 INSERT INTO Course VALUES(01 , '語文' , 02); INSERT INTO Course VALUES(02 , '數學' , 01); INSERT INTO Course VALUES(03 , '英語' , 03); -- 教師表測試資料 INSERT INTO Teacher VALUES(01 , '張三'); INSERT INTO Teacher VALUES(02 , '李四'); INSERT INTO Teacher VALUES(03 , '王五'); INSERT INTO Teacher VALUES(04 , '李六'); COMMIT; -- 成績表測試資料 INSERT INTO SC VALUES(01 , 01 , 80); INSERT INTO SC VALUES(01 , 02 , 90); INSERT INTO SC VALUES(01 , 03 , 99); INSERT INTO SC VALUES(02 , 01 , 70); INSERT INTO SC VALUES(02 , 02 , 60); INSERT INTO SC VALUES(02 , 03 , 80); INSERT INTO SC VALUES(03 , 01 , 80); INSERT INTO SC VALUES(03 , 02 , 80); INSERT INTO SC VALUES(03 , 03 , 80); INSERT INTO SC VALUES(04 , 01 , 50); INSERT INTO SC VALUES(04 , 02 , 30); INSERT INTO SC VALUES(04 , 03 , 20); INSERT INTO SC VALUES(05 , 01 , 76); INSERT INTO SC VALUES(05 , 02 , 87); INSERT INTO SC VALUES(06 , 01 , 31); INSERT INTO SC VALUES(06 , 03 , 34); INSERT INTO SC VALUES(07 , 02 , 89); INSERT INTO SC VALUES(07 , 03 , 98); INSERT INTO SC VALUES(08 , 01 , 31); INSERT INTO SC VALUES(08 , 03 , 34); INSERT INTO SC VALUES(08 , 02 , 89); -- Student(S#,Sname,Sage,Ssex) 學生表 -- Course(C#,Cname,T#) 課程表 -- SC(S#,C#,score) 成績表 -- Teacher(T#,Tname) 教師表 -- 1、查詢“01”課程比“02”課程成績高的所有學生的學號; SELECT a.sid FROM (SELECT sid, score FROM sc WHERE cid=01) a,(SELECT sid ,score FROM sc WHERE cid=02) b WHERE a.score > b.score AND a.sid= b.sid; SELECT a.sid FROM student a LEFT JOIN SC b ON b.cid=01 AND a.sid=b.sid LEFT JOIN SC c ON c.cid=02 AND a.sid=c.sid WHERE b.score>c.score; -- 2、查詢平均成績大於60分的同學的學號和平均成績; SELECT sid, AVG(score) FROM sc GROUP BY sid HAVING AVG(score)>60; -- 3、查詢所有同學的學號、姓名、選課數、總成績; SELECT s.sid, sname, COUNT(cid), AVG(score) FROM student s LEFT JOIN sc ON s.sid = sc.sid GROUP BY sc.sid; -- 4、查詢姓“李”的老師的個數; SELECT COUNT(DISTINCT(tname)) FROM Teacher WHERE tname LIKE '李%'; -- 5、查詢沒學過“王五”老師課的同學的學號、姓名; SELECT sid, sname FROM student WHERE sid NOT IN(SELECT DISTINCT(s.sid) FROM student s, Course c, Teacher t, sc WHERE s.sid = sc.sid AND sc.cid = c.cid AND c.tid = t.tid AND t.tname = '王五'); -- 效率低 SELECT sid, sname FROM student s1 LEFT JOIN (SELECT DISTINCT(s.sid) id FROM student s, Course c, Teacher t, sc WHERE s.sid = sc.sid AND sc.cid = c.cid AND c.tid = t.tid AND t.tname = '王五') AS s2 ON s1.sid = s2.id WHERE s2.id IS NULL; -- 效率相對高 -- 6、查詢學過“01”並且也學過編號“02”課程的同學的學號、姓名; SELECT DISTINCT(s.sid), sname FROM student s, Course c, SC WHERE s.sid = sc.sid AND c.cid = sc.cid AND c.cid = 01; SELECT DISTINCT(s.sid), sname FROM student s, Course c, SC WHERE s.sid = sc.sid AND c.cid = sc.cid AND c.cid = 02; SELECT DISTINCT(s.sid), sname FROM student s, Course c, SC WHERE s.sid = sc.sid AND c.cid = sc.cid AND c.cid = 01 AND s.sid IN (SELECT sid FROM SC WHERE cid = 02); -- 7、查詢學過“王五”老師所教的所有課的同學的學號、姓名; SELECT s.sid, sname FROM Student s, Course c, Teacher t, SC WHERE s.sid = sc.sid AND c.cid = sc.cid AND t.tid = c.tid AND t.tname = '王五'; -- 8、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名; SELECT s.sid, sname FROM student s, (SELECT sid, score FROM SC WHERE cid = 01) sc1, (SELECT sid, score FROM SC WHERE cid = 02) sc2 WHERE sc1.sid = s.sid AND sc2.sid = s.sid AND sc1.score > sc2.score; -- 9、查詢所有課程成績小於60分的同學的學號、姓名; SELECT sid, sname FROM student WHERE sid NOT IN (SELECT sid FROM sc WHERE sc.score > 60); -- 10、查詢沒有學全所有課的同學的學號、姓名; SELECT sid, sname FROM student WHERE sid IN(SELECT sid FROM sc GROUP BY sid HAVING COUNT(sid) < (SELECT COUNT(cid) FROM Course)); -- 巢狀,效率相對低 SELECT s.sid, s.sname FROM student s, sc WHERE s.sid = sc.sid GROUP BY sc.sid HAVING COUNT(sc.sid) < (SELECT COUNT(cid) FROM Course); -- 11、查詢至少有一門課與學號為“08”的同學所學相同的同學的學號和姓名; SELECT DISTINCT(s.sid), s.sname FROM student s, sc WHERE s.sid = sc.sid AND cid IN(SELECT cid FROM sc WHERE sid = 08); -- 巢狀 -- 12、查詢至少學過學號為“01”同學所有一門課的其他同學學號和姓名; SELECT DISTINCT(s.sid), s.sname FROM student s, sc WHERE s.sid = sc.sid AND s.sid <> 08 AND cid IN(SELECT cid FROM sc WHERE sid = 08); -- 13、把“SC”表中“王五”老師教的課的成績都更改為此課程的平均成績;--------xxx UPDATE SC SET score = ( SELECT a.avgs FROM ( SELECT AVG(score) avgs FROM Course c, Teacher t,SC WHERE c.cid = sc.cid AND c.tid = t.tid AND t.tname = '王五') a ) WHERE sc.cid =( SELECT b.cid FROM ( SELECT DISTINCT c.cid FROM Course c, Teacher t,SC WHERE c.cid = sc.cid AND c.tid = t.tid AND t.tname = '王五') b ); SELECT * FROM sc s LEFT JOIN course c ON s.cid=c.cid LEFT JOIN teacher t ON t.tid=c.tid WHERE t.tname='王五'; SELECT s.sid,AVG(s.score) AVG,t.tname FROM sc s LEFT JOIN course c ON s.cid=c.cid LEFT JOIN teacher t ON t.tid=c.tid WHERE t.tname='王五' GROUP BY c.cid; UPDATE sc SET score=( SELECT a.avg FROM ( SELECT AVG(s.score) AVG FROM sc s LEFT JOIN course c ON s.cid=c.cid LEFT JOIN teacher t ON t.tid=c.tid WHERE t.tname='王五' GROUP BY c.cid ) a ) WHERE sid IN ( SELECT b.sid FROM ( SELECT s.sid FROM sc s LEFT JOIN course c ON s.cid=c.cid LEFT JOIN teacher t ON t.tid=c.tid WHERE t.tname='王五' )b ); -- 14、查詢和“02”號的同學學習的課程完全相同的其他同學學號和姓名; -- 重點記一下 SELECT s.sid, s.sname FROM student s, sc WHERE s.sid = sc.sid AND sc.cid IN (SELECT cid FROM sc WHERE sid = 02) GROUP BY sc.sid HAVING COUNT(sc.sid) = (SELECT COUNT(sid) FROM sc WHERE sid = 02); -- 15、刪除學習“王五”老師課的SC表記錄; DELETE FROM sc WHERE cid = (SELECT cid FROM course c, teacher t WHERE c.tid = t.tid AND tname = '王五'); -- 16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號03課程的同學學號、02號課的平均成績;-----回過頭來弄懂! INSERT INTO sc SET sid = (SELECT DISTINCT(sid) FROM sc WHERE cid <> 03), cid = 03, score = (SELECT AVG(score) FROM sc WHERE sid = 02)); INSERT SC SELECT Sid, 02,(SELECT AVG(score) FROM SC WHERE Cid=02) FROM Student WHERE Sid NOT IN (SELECT Sid FROM SC WHERE Cid=02); -- 17、按平均成績從高到低顯示所有學生的“數學”、“英語”、“語文”三門的課程成績,按如下形式顯示:學生ID,數學,英語,語文,有效課程數,有效平均分 ------ SELECT Sid AS 學生ID ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid=02) AS 數學 ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid=03) AS 英語 ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid=01) AS 語文 ,COUNT(*) AS 有效課程數, AVG(t.score) AS 平均成績 FROM SC AS t GROUP BY Sid ORDER BY AVG(t.score); -- 18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分 SELECT DISTINCT l.cid AS 課程ID, h.score AS 最高分, l.score AS 最低分 FROM sc AS h ,sc AS l WHERE h.cid = l.cid AND h.score =(SELECT MAX(score) FROM sc WHERE cid = l.cid) AND l.score = (SELECT MIN(score) FROM sc WHERE cid = l.cid); -- 19、按各科平均成績從低到高和及格率的百分數從高到低順序查詢 -- 100 * SUM(CASE WHEN ISNULL(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分數 SELECT cid AS 課程號, AVG(score) AS 平均成績, 100 * SUM(CASE WHEN score >=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分數 FROM sc GROUP BY cid ORDER BY (100 * SUM(CASE WHEN score>=60 THEN 1 ELSE 0 END)/COUNT(*)) DESC; -- 20、查詢如下課程平均成績和及格率的百分數(用"1行"顯示): 語文,英語 SELECT cid AS 課程號, AVG(score) AS 平均成績, 100 * SUM(CASE WHEN score >=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分數 FROM sc WHERE cid = 01 OR cid = 03 GROUP BY cid; -- 21、查詢不同老師所教不同課程平均分從高到低顯示 SELECT tname 老師姓名, cname 課程名, AVG(score) 平均成績 FROM teacher t, course c, sc WHERE t.tid = c.tid AND c.cid = sc.cid GROUP BY t.tid ORDER BY AVG(score) DESC; -- 22、查詢如下課程成績第3名到第6名的學生成績單:語文(01),英語(03) 格式如下:[學生ID], [學生姓名], 語文, 英語, 平均成績 SELECT s.sid, s.sname,IFNULL(cn.score,0) 語文, IFNULL(en.score,0) 英語, (IFNULL(cn.score,0)+IFNULL(en.score,0))/2 平均分 FROM student s LEFT JOIN sc cn ON cn.sid = s.sid AND cn.cid = 01 LEFT JOIN sc en ON en.sid = s.sid AND en.cid = 03 ORDER BY 平均分 DESC LIMIT 3,3; -- 23、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60] SELECT c.cid, c.cname, SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 優秀, SUM(CASE WHEN score BETWEEN 75 AND 85 THEN 1 ELSE 0 END) AS 良好, SUM(CASE WHEN score BETWEEN 60 AND 75 THEN 1 ELSE 0 END) AS 及格, SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS 不及格 FROM course c, sc WHERE c.cid = sc.cid GROUP BY sc.cid; SELECT c.cid AS 課程ID, c.cname AS 課程名稱, SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 優秀, SUM(CASE WHEN score BETWEEN 75 AND 85 THEN 1 ELSE 0 END) AS 良好, SUM(CASE WHEN score BETWEEN 60 AND 75 THEN 1 ELSE 0 END) AS 及格, SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) 不及格 FROM course c, sc WHERE c.cid = sc.cid GROUP BY c.cid; -- 24、查詢學生平均成績及其名次 SELECT sid 學號, AVG(score) 平均成績 FROM sc GROUP BY sid ORDER BY AVG(score) DESC; SELECT 1+(SELECT COUNT( DISTINCT 平均成績) FROM (SELECT sid,AVG(score) 平均成績 FROM SC GROUP BY sid ) t1 WHERE 平均成績 > t2.平均成績) 名次, sid AS 學生學號, 平均成績 FROM (SELECT sid,AVG(score) 平均成績 FROM sc GROUP BY sid ) t2 ORDER BY 平均成績 DESC; SELECT 1+(SELECT COUNT( DISTINCT 平均成績) FROM (SELECT Sid,AVG(score) AS 平均成績 FROM SC GROUP BY Sid ) AS T1 WHERE 平均成績 > T2.平均成績) AS 名次, Sid AS 學生學號,平均成績 FROM (SELECT Sid,AVG(score) 平均成績 FROM SC GROUP BY Sid ) AS T2 ORDER BY 平均成績 DESC; -- 25、查詢各科成績前三名的記錄:(不考慮成績並列情況) --------------xxxxx SELECT cid 課程ID, score 成績 FROM sc ORDER BY cid LIMIT 0,3; SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 1 ORDER BY score DESC LIMIT 0,3; SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 2 ORDER BY score DESC LIMIT 0,3; SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 3 ORDER BY score DESC LIMIT 0,3; -- 26、查詢每門課程被選修的學生數 SELECT cid, COUNT(sid) FROM sc GROUP BY cid; -- 27、查詢出只選修了兩門課程的全部學生的學號和姓名 SELECT s.sid, s.sname FROM student s, sc WHERE s.sid = sc.sid GROUP BY sc.sid HAVING COUNT(sc.sid) = 2; -- 28、查詢男生、女生人數 SELECT COUNT(sid) 男生人數 FROM student WHERE ssex = '男'; SELECT COUNT(sid) 女生人數 FROM student WHERE ssex = '女'; -- 29、查詢姓“王”的學生名單 SELECT * FROM student WHERE sname LIKE '王%'; -- 30、查詢同名同性學生名單,並統計同名人數 SELECT sname 姓名, COUNT(*) 人數 FROM student GROUP BY sname,ssex HAVING COUNT(*) >= 2; -- 31、1981年出生的學生名單(注:Student表中Sage列的型別是DATETIME) 原題目答案: SELECT Sname, CONVERT(CHAR (11),DATEPART(YEAR,Sage)) AS age FROM student WHERE CONVERT(CHAR(11),DATEPART(YEAR,Sage))='1981'; -- 改成19歲的人的名單 SELECT sname FROM student WHERE sage = 19; -- 32、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列 SELECT cid 課程號, AVG(score) 平均成績 FROM sc GROUP BY cid ORDER BY AVG(score), cid DESC; -- 33、查詢平均成績大於75的所有學生的學號、姓名和平均成績 SELECT s.sid, sname, AVG(score) FROM student s, sc WHERE s.sid = sc.sid GROUP BY s.sid HAVING AVG(score) > 75; -- 34、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數 SELECT sname, score FROM student s, course c, sc WHERE s.sid = sc.sid AND c.cid = sc.cid AND cname='數學' AND score < 60; -- 35、查詢所有學生的選課情況; SELECT s.sid, sc.cid, s.sname, c.cname FROM student s, course c, sc WHERE s.sid = sc.sid AND c.cid = sc.cid; -- 36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數; SELECT s.sname, c.cname, score FROM student s, course c ,sc WHERE s.sid = sc.sid AND c.cid = sc.cid AND score > 70; -- 37、查詢不及格的課程,並按課程號從大到小排列 SELECT s.sname, c.cid, c.cname, score FROM student s, course c, sc WHERE s.sid = sc.sid AND c.cid = sc.cid AND score < 60 ORDER BY cid DESC; -- 38、查詢課程編號為02且課程成績在80分以上的學生的學號和姓名; SELECT s.sid, s.sname FROM student s, sc WHERE s.sid = sc.sid AND score > 80 AND sc.cid=02; -- 39、求選了課程的學生人數 SELECT COUNT(a.s) 選了課程的人數 FROM (SELECT COUNT(sid) s FROM sc GROUP BY sid) a; -- 去除了重複的學生 SELECT COUNT(*) FROM sc; -- 不去除重複學生 -- 40、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績 SELECT sname, MAX(score) FROM student s, course c, teacher t, sc WHERE s.sid = sc.sid AND c.cid = sc.cid AND c.tid = t.tid AND tname = '張三'; -- 41、查詢各個課程及相應的選修人數 SELECT cid, COUNT(*) FROM sc GROUP BY cid; -- 42、查詢不同課程成績相同的學生的學號、課程號、學生成績 SELECT sid, cid, score FROM sc WHERE score IN (SELECT score FROM sc GROUP BY score HAVING COUNT(score) >= 2) ORDER BY score; -- 所有人之間對比 SELECT DISTINCT A.Sid,B.score FROM SC A ,SC B WHERE A.Score=B.Score AND A.Cid <>B.Cid ; -- 單人成績對比 -- 43、查詢每門課程成績最好的前兩名 SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 1 ORDER BY score DESC LIMIT 0,2; SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 2 ORDER BY score DESC LIMIT 0,2; SELECT sid 學號, cid 課程ID, score 成績 FROM sc WHERE cid = 3 ORDER BY score DESC LIMIT 0,2; -- 44、統計每門課程的學生選修人數(超過7人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列 SELECT cid 課程ID, COUNT(*) 人數 FROM sc GROUP BY cid HAVING COUNT(*) >7 ORDER BY 人數, 課程ID DESC; -- 45、檢索至少選修兩門課程的學生學號 SELECT sid FROM sc GROUP BY sid HAVING COUNT(sid) >=2; -- 46、查詢全部學生都選修的課程的課程號和課程名 SELECT c.cid, c.cname,COUNT(sc.sid) FROM course c, sc WHERE c.cid = sc.cid GROUP BY c.cid HAVING COUNT(sc.sid) = (SELECT COUNT(*) FROM student); -- 47、查詢沒學過“王五”老師講授的任一門課程的學生姓名 SELECT s1.sname FROM student s1 WHERE s1.sname NOT IN( SELECT s.sname FROM student s, course c, teacher t, sc WHERE s.sid = sc.sid AND c.cid = sc.cid AND c.tid = t.tid AND t.tname = '王五'); -- 48、查詢兩門以上不及格課程的同學的學號及其平均成績 SELECT sid, AVG(score) FROM sc WHERE sid IN (SELECT sid FROM sc WHERE score < 60 GROUP BY sid HAVING COUNT(*)>=2); -- 49、檢索“01”課程分數小於60,按分數降序排列的同學學號 SELECT sid, score FROM sc WHERE cid = 01 AND score < 60 ORDER BY score DESC; -- 50、刪除“02”同學的“01”課程的成績 DELETE FROM sc WHERE sid = 02 AND cid = 01;