mysql 常用sql語句總結
1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
SELECT Sname,Ssex,Class FROM student
select sname,ssex,class from student
2、 查詢教師所有的單位即不重複的Depart列。
SELECT DISTINCT Depart FROM teacher
select distanct depart from teacher;
3、 查詢Student表的所有記錄。
SELECT * FROM student
select * from student
4、 查詢Score表中成績在60到80之間的所有記錄。
SELECT * FROM Score WHERE Degree BETWEEN 60 AND 80
select * form score where degree between 60 and 80
5、 查詢Score表中成績為85,86或88的記錄。
SELECT * FROM Score WHERE Degree IN(85,86,88)
select * from score where degree in(85,86,88)
6、 查詢Student表中“95031”班或性別為“女”的同學記錄。
SELECT * FROM Student WHERE class='95031' OR Ssex='女'
welect * from student where class='95031' or ssex='女'
7、 以Class降序查詢Student表的所有記錄。
SELECT * FROM student ORDER BY class DESC
select * from student order by class desc/asc
8、 以Cno升序、Degree降序查詢Score表的所有記錄。
SELECT * FROM Score ORDER BY cno ASC,degree DESC
select * from score order by cno asc,degree desc
9、 查詢“95031”班的學生人數。
SELECT COUNT(*) FROM student WHERE class='95031'
10、查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
SELECT Sno,Cno FROM Score WHERE Degree=(SELECT MAX(Degree) FROM Score);
SELECT Sno,Cno FROM Score ORDER BY Degree DESC LIMIT 0,1;
11、查詢每門課的平均成績。
SELECT Cno,AVG(degree) FROM Score GROUP BY Cno;
12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
SELECT AVG(Degree) FROM score WHERE Cno LIKE '3%' AND Cno IN (SELECT Cno FROM score GROUP BY Cno HAVING COUNT(*)>=5) 用in 不用= 是因為可能會有多個
簡單寫法:select AVG(Degree) FROM score WHERE Cno LIKE '3%' AND GROUP BY Cno HAVING COUNT(*)>=5;
13、查詢分數大於70,小於90的Sno列。
SELECT Sno FROM Score WHERE degree>70 AND degree<90;
14、查詢所有學生的Sname、Cno和Degree列。
SELECT Sname, Cno,Degree FROM Score , student WHERE Score.Sno=student.Sno;
15、查詢所有學生的Sno、Cname和Degree列。
SELECT Sno,Cname,Degree FROM Score , Course WHERE Score.Cno=Course.Cno;
16、查詢所有學生的Sname、Cname和Degree列。
SELECT Sname,Cname,Degree FROM student,course,score WHERE student.Sno=score.Sno AND course.Cno=score.Cno
JOIN .. ON 寫法:select Sname,Cname,Degree FROM student JOIN score ON student.Sno=score.Sno JOIN course ON course.Cno=score.Cno;
17、查詢“95033”班學生的平均分。
SELECT AVG(degree) AS 'class=95033' FROM Score WHERE Sno IN (SELECT Sno FROM Student 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’)
現查詢所有同學的Sno、Cno和rank列。
SELECT Sno,Cno,rank FROM Score,grade WHERE degree BETWEEN low AND upp;
19、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
<span style="color: #000000; font-size: 15px">109同學,選修是3-105課的</span>
SELECT * FROM score WHERE Cno='3-105' AND degree>(SELECT MAX(degree ) FROM Score WHERE Sno='109' AND Cno='3-105' )
<span style="font-size: 15px">109同學,沒有選修3-105課</span>
SELECT * FROM score WHERE Cno='3-105' AND degree>(SELECT MAX(degree ) FROM Score WHERE Sno='109')
AND degree<( SELECT MAX(degree ) FROM Score WHERE sno IN (SELECT Sno FROM score GROUP BY Sno HAVING COUNT(*)>1))
20、選了多門課程並且是這個課程下不是最高分的
SELECT * FROM score a WHERE Sno IN (SELECT Sno FROM score GROUP BY Sno HAVING COUNT(*)>1) AND degree<( SELECT MAX(degree ) FROM Score b WHERE b.cno = a.cno)
21、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
SELECT * FROM score WHERE degree>(SELECT degree FROM Score WHERE Sno='109' AND Cno='3-105' )
22、查詢和學號為108、101的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) = (SELECT YEAR(sbirthday) FROM student WHERE sno='108')
SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) = (SELECT YEAR(sbirthday) FROM student WHERE sno='101')
23、查詢“張旭“教師任課的學生成績。
SELECT Sno,degree FROM score,Course WHERE score.Cno=Course.Cno AND Course.Tno= (SELECT Tno FROM Teacher WHERE Tname='張旭' )
SELECT degree FROM score WHERE Cno IN (SELECT cno FROM course WHERE Tno= (SELECT Tno FROM Teacher WHERE Tname='張旭' ) )
24、查詢選修某課程的同學人數多於5人的教師姓名。
SELECT Tname FROM Teacher, Course WHERE Teacher.Tno=Course.Tno AND Course.Cno =(SELECT Cno FROM Score GROUP BY Cno HAVING COUNT(*)>5)
SELECT Tname FROM Teacher WHERE tno=( SELECT Tno FROM Course WHERE cno=( SELECT Cno FROM Score GROUP BY Cno HAVING COUNT(*)>5 ))
25、查詢95033班和95031班全體學生的記錄。
SELECT * FROM student WHERE class IN ('95033','95031')
26、 查詢存在有85分以上成績的課程Cno.
SELECT Cno FROM score WHERE degree>85
27、查詢出“計算機系“教師所教課程的成績表。
SELECT * FROM course WHERE cno IN (SELECT cno FROM course WHERE tno IN (SELECT tno FROM teacher WHERE Depart='計算機系'))
28、查詢“計算 機系”與“電子工程系“不同職稱的教師的Tname和Prof。
SELECT Tname,Prof FROM Teacher WHERE Depart ='計算機系' AND Prof NOT IN( SELECT Prof FROM Teacher WHERE Depart ='電子工程系')<br>UNION <br>SELECT Tname,Prof FROM Teacher WHERE Depart ='電子工程系' AND Prof NOT IN( SELECT Prof FROM Teacher WHERE Depart ='計算機系')
29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。
ANY:代表括號中任意一個成績就可以
SELECT Cno,Sno,Degree FROM score WHERE cno='3-105' AND degree >ANY(SELECT degree FROM score WHERE cno='3-245' ) ORDER BY degree DESC
30、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.
ALL:代表括號中的所有成績
SELECT Cno,Sno,Degree FROM score WHERE cno='3-105' AND degree >ALL(SELECT degree FROM score WHERE cno='3-245' ) ORDER BY degree DESC
31、 查詢所有教師和同學的name、sex和birthday.
SELECT tname,tsex,tbirthday FROM Teacher UNION SELECT sname,ssex,sbirthday FROM Student
32、查詢所有“女”教師和“女”同學的name、sex和birthday.
SELECT Tname,Tsex,Tbirthday FROM Teacher WHERE Tsex='女' UNION SELECT Sname,Ssex,Sbirthday FROM Student WHERE Ssex='女'
33、 查詢成績比該課程平均成績低的同學的成績表。
SELECT * FROM score a WHERE degree < (SELECT AVG(degree) FROM score b WHERE b.cno=a.cno)
34、 查詢所有任課教師的Tname和Depart.
SELECT Tname,Depart FROM Teacher WHERE tno IN (SELECT tno FROM course )
35 、 查詢所有未講課的教師的Tname和Depart.
SELECT Tname,Depart FROM Teacher WHERE Tno NOT IN (SELECT Tno FROM Course WHERE cno IN (SELECT cno FROM score ))
36、查詢至少有2名男生的班號。
SELECT class FROM student WHERE ssex='男' GROUP BY class HAVING COUNT(*)>1
37、查詢Student表中不姓“王”的同學記錄。
SELECT * FROM Student WHERE Sname NOT LIKE '王%%'
38、查詢Student表中每個學生的姓名和年齡。
SELECT Sname, YEAR(NOW())-YEAR(sbirthday) FROM Student
39、查詢Student表中最大和最小的Sbirthday日期值。
SELECT MAX(Sbirthday ),MIN(Sbirthday ) FROM Student
40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。
SELECT * FROM Student ORDER BY class DESC, Sbirthday
41、查詢“男”教師及其所上的課程。
SELECT Tname,Cname FROM course,teacher WHERE course.tno= teacher.tno AND teacher.Tsex='男'
42、查詢最高分同學的Sno、Cno和Degree列。
SELECT Sno,Cno,Degree FROM score WHERE degree=(SELECT MAX(degree) FROM score)
排序寫法:
SELECT Sno,Cno,Degree FROM score ORDER BY degree DESC LIMIT 0,1
43、查詢和“李軍”同性別的所有同學的Sname.
SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname='李軍')
44、查詢和“李軍”同性別並同班的同學Sname.
SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname='李軍' ) AND class=( SELECT class FROM student WHERE Sname='李軍')
45、查詢所有選修“計算機導論”課程的“男”同學的成績表。
SELECT Sno,Cno,degree FROM score WHERE Cno=( SELECT Cno FROM course WHERE Cname='計算機導論') AND Sno IN (SELECT Sno FROM student WHERE Ssex='男')