标签:题目 插入 百分数 distinct like 就会 设计 类型 学习
(1)分清HAVING与WHERE的区别:
HAVING 子句使你能够指定过滤条件,从而控制查询结果中哪些组可以出现在最终结果里面。WHERE 子句对被选择的列施加条件,而 HAVING 子句则对 GROUP BY 子句所产生的组施加条件。
以下的SQL语句都是基于MySQL5.6.30版本。
select a.Sno from (select Sno,score from SC where Cno=1 ) as a, (select Sno,score from SC where Cno=2 ) as b where a.score>b.score and a.Sno=b.Sno;
SELECT Sno,AVG(score) FROM SC GROUP BY Sno HAVING AVG(score) >60;
SELECT Student.Sno,Student.Sname,COUNT(SC.Cno),SUM(SC.score) FROM Student LEFT OUTER JOIN SC ON Student.Sno=SC.Sno GROUP BY Student.Sno,Sname;
group by是在左外连接的基础上进行分组。注意是查询所有同学的,使用如下就会使用内连接,如果一些学生无成绩将不显示。与题目不符合
SELECT COUNT(DISTINCT(Tname)) FROM Teacher WHERE Tname LIKE ‘李%‘;
SELECT Student.Sno,Student.Sname FROM Student WHERE Sno NOT IN ( SELECT DISTINCT(SC.Sno) FROM SC,Course,Teacher WHERE SC.Cno=Course.Cno AND Teacher.Tno=Course.Tno AND Teacher.Tname=‘李小风‘ );
SELECT Student.Sno,Student.Sname FROM Student,SC WHERE Student.Sno=SC.Sno AND SC.Cno=1 AND EXISTS( SELECT * FROM SC AS SC_2 WHERE SC_2.Sno=SC.Sno AND SC_2.Cno=2 ) -- 查询同时学过1、2、3的课程 -- AND EXISTS( SELECT * FROM SC AS SC_3 WHERE SC_3.Sno=SC.Sno AND SC_3.Cno=3 )
SELECT Sno,Sname FROM Student WHERE Sno IN ( SELECT Sno FROM SC,Course ,Teacher WHERE SC.Cno=Course.Cno AND Teacher.Tno=Course.Tno AND Teacher.Tname=‘李小风‘ GROUP BY Sno HAVING COUNT(SC.Cno)=( SELECT COUNT(Cno) FROM Course,Teacher WHERE Teacher.Tno=Course.Tno AND Tname=‘李小风‘ ) );
SELECT Sno,Sname FROM Student WHERE Sno NOT IN ( SELECT Student.Sno FROM Student,SC WHERE Student.Sno=SC.Sno AND score>60 );
SELECT Student.Sno,Student.Sname FROM Student,SC WHERE Student.Sno=SC.Sno GROUP BY Student.Sno,Student.Sname HAVING COUNT(SC.Cno) <(SELECT COUNT(Course.Cno) FROM Course );
SELECT s.Sno,s.Sname FROM Student s GROUP BY s.Sno,s.Sname HAVING s.Sno IN ( SELECT Course.Cno FROM Course,SC WHERE SC.Sno=1 ) SELECT s.Sno,s.Sname FROM Student s,SC sc WHERE s.Sno=sc.Sno AND sc.Cno IN ( SELECT Course.Cno FROM Course,SC WHERE SC.Sno=1 ) GROUP BY s.Sno
UPDATE SC,Course,Teacher SET SC.score=( SELECT AVG(SC_2.score) FROM (SELECT * FROM SC) AS SC_2 WHERE SC_2.Cno=SC.Cno ) WHERE Course.Cno=SC.Cno AND Course.Tno=Teacher.Tno AND Teacher.Tname=‘李力‘;
SELECT Sno FROM SC WHERE Cno IN(SELECT Cno FROM SC WHERE Sno=2) GROUP BY Sno HAVING COUNT(*)=(SELECT COUNT(*) FROM SC WHERE Sno=2);
DELETE SC FROM Course,Teacher,SC WHERE Course.Cno=SC.Cno AND Course.Tno=Teacher.Tno AND Tname=‘李力‘;
INSERT SC SELECT Sno,2, ( SELECT AVG(score) FROM SC WHERE Cno=2 ) FROM Student WHERE Sno NOT IN (SELECT Sno FROM SC WHERE Cno=3);
SELECT Sno AS 学生ID ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=4) AS 高数 ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=1) AS C语言 ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=6) AS Java程序高级设计 ,COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩 FROM SC AS t GROUP BY Sno ORDER BY AVG(t.score)
SELECT SC.Cno,MAX(score),MIN(score) FROM SC GROUP BY Cno
SELECT t.Cno AS 课程号, MAX(c.Cname) AS 课程名, IFNULL(AVG(t.score),0) AS 平均成绩, 100 * SUM(CASE WHEN IFNULL(t.score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC t,Course c WHERE t.Cno=c.Cno GROUP BY t.Cno ORDER BY 及格百分数 DESC
SELECT MAX(Z.Tno) AS 教师ID, MAX(Z.Tname) AS 教师姓名, C.Cno AS 课程ID, C.Cname AS 课程名称, AVG(Score) AS 平均成绩 FROM SC AS T,Course AS C ,Teacher AS Z WHERE T.Cno=C.Cno AND C.Tno=Z.Tno GROUP BY C.Cno ORDER BY 平均成绩 DESC
SELECT SC.Sno AS 学生学号, Student.Sname AS 学生姓名, T1.score AS 高数, T2.score AS C语言, T3.score AS Java高级程序设计, T4.score AS 数据结构与算法, IFNULL(T1.score,0) +IFNULL(T2.score,0) +IFNULL(T3.score,0) +IFNULL(T4.score,0) AS 总分 FROM Student,SC LEFT JOIN SC AS T1 ON SC.Sno = T1.Sno AND T1.Cno =1 LEFT JOIN SC AS T2 ON SC.Sno = T2.Sno AND T2.Cno =2 LEFT JOIN SC AS T3 ON SC.Sno = T3.Sno AND T3.Cno =3 LEFT JOIN SC AS T4 ON SC.Sno = T4.Sno AND T4.Cno =4 WHERE Student.Sno=SC.Sno GROUP BY SC.Sno ORDER BY 总分 DESC LIMIT 3,3
SELECT SC.Cno AS 课程ID, Cname AS 课程名称 ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS ‘[100 - 85]‘ ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS ‘[85 - 70]‘ ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS ‘[70 - 60]‘ ,SUM(CASE WHEN score <60 THEN 1 ELSE 0 END) AS ‘[60 -]‘ FROM SC,Course WHERE SC.Cno=Course.Cno GROUP BY SC.Cno,Cname;
SELECT 1+( SELECT COUNT(DISTINCT 平均成绩) FROM ( SELECT Sno,AVG(score) AS 平均成绩 FROM SC GROUP BY Sno ) AS T1 WHERE 平均成绩 > T2.平均成绩 ) AS 名次, Sno AS 学生学号,平均成绩 FROM (SELECT Sno,AVG(score) AS 平均成绩 FROM SC GROUP BY Sno) AS T2 ORDER BY 平均成绩 DESC;
SELECT t1.Sno AS 学生ID,t1.Cno AS 课程ID,Score AS 分数 FROM SC t1 WHERE score IN ( SELECT score FROM SC WHERE t1.Cno=Cno ORDER BY score DESC LIMIT 0,3 ) ORDER BY t1.Cno;
不支持子查询中使用limit关键字,报错如下:
This version of MySQL doesn‘t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery‘
SELECT SC.Sno,Student.Sname,COUNT(Cno) AS 选课数 FROM SC ,Student WHERE SC.Sno=Student.Sno GROUP BY SC.Sno ,Student.Sname HAVING COUNT(Cno)=1;
SELECT Sname,CAST(DATE_FORMAT(Sbirthday,‘%Y-%m-%d‘) AS CHAR(13)) AS birthday FROM Student WHERE CAST(DATE_FORMAT(Sbirthday,‘%Y-%m-%d‘) AS CHAR(13))=‘2015-09-16‘ ;
SELECT Cno,AVG(score) FROM SC GROUP BY Cno ORDER BY AVG(score),Cno DESC ;
SELECT Student.Sname,score FROM Student,SC,Course C,Teacher WHERE Student.Sno=SC.Sno AND SC.Cno=C.Cno AND C.Tno=Teacher.Tno AND Teacher.Tname=‘李小风‘ AND SC.score=(SELECT MAX(score) FROM SC WHERE Cno=C.Cno ); -- 如下的语句是错误的 SELECT Student.Sname,score FROM Student,SC,Course C,Teacher WHERE Student.Sno=SC.Sno AND SC.Cno=C.Cno AND C.Tno=Teacher.Tno AND Teacher.Tname=‘李小风‘ ORDER BY SC.score DESC LIMIT 1
第二条语句当李小风老师教授多个课程时,查询出来的结果应该是多条的。
select std.Sno,std.Sname from student std,SC sc where std.Sno=sc.Sno group by std.Sno having count(distinct(sc.Cno))>2
count()中可以使用distinct()函数
标签:题目 插入 百分数 distinct like 就会 设计 类型 学习
原文地址:http://www.cnblogs.com/mazhimazhi/p/7499372.html