美文网首页MySQL
Mysql Sql 语句练习题mysql 数据库练习题

Mysql Sql 语句练习题mysql 数据库练习题

作者: Guyone | 来源:发表于2020-02-28 14:50 被阅读0次

    一:表与数据

    CREATE DATABASE db01;

    USE db01;

    -- 学生表 student

    CREATE TABLE student(Sid VARCHAR(10),Sname NVARCHAR(10),Sage DATETIME,Ssex NVARCHAR(10))

    INSERT INTO student VALUES('01' , N'赵雷' , '1990-01-01' , N'男'),

    ('02' , N'钱电' , '1990-12-21' , N'男'),

    ('03' , N'孙风' , '1990-05-20' , N'男'),

    ('04' , N'李云' , '1990-08-06' , N'男'),

    ('05' , N'周梅' , '1991-12-01' , N'女'),

    ('06' , N'吴兰' , '1992-03-01' , N'女'),

    ('07' , N'郑竹' , '1989-07-01' , N'女'),

    ('08' , N'王菊' , '1990-01-20' , N'女');

    -- 科目表 course

    CREATE TABLE course(Cid VARCHAR(10),Cname NVARCHAR(10),Tid VARCHAR(10))

    INSERT INTO course VALUES('01' , N'语文' , '02'),('02' , N'数学' , '01'),('03' , N'英语' , '03');

    -- 教师表 teacher

    CREATE TABLE teacher(Tid VARCHAR(10),Tname NVARCHAR(10))

    INSERT INTO teacher VALUES('01' , N'张三'),('02' , N'李四'),('03' , N'王五');

    -- 成绩表 sc

    CREATE TABLE sc(Sid VARCHAR(10),Cid VARCHAR(10),score DECIMAL(18,1))

    INSERT INTO sc VALUES('01' , '01' , 80)

    ,('01' , '02' , 90)

    ,('01' , '03' , 99)

    ,('02' , '01' , 70)

    ,('02' , '02' , 60)

    ,('02' , '03' , 80)

    ,('03' , '01' , 80)

    ,('03' , '02' , 80)

    ,('03' , '03' , 80)

    ,('04' , '01' , 50)

    ,('04' , '02' , 30)

    ,('04' , '03' , 20)

    ,('05' , '01' , 76)

    ,('05' , '02' , 87)

    ,('06' , '01' , 31)

    ,('06' , '03' , 34)

    ,('07' , '02' , 89)

    ,('07' , '03' , 98);

    二练习题:

    -- 1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

    SELECT a.Sid,a.score,student.* FROM (SELECT * FROM sc WHERE sc.`Cid`=01 ) a

    LEFT JOIN (SELECT * FROM sc WHERE sc.cid=02)b ON a.Sid=b.Sid

    LEFT JOIN student ON a.Sid=student.Sid

    WHERE a.score>b.score;

    -- 1.1 查询同时存在" 01 "课程和" 02 "课程的情况

    SELECT * FROM (SELECT * FROM sc WHERE sc.`Cid`=01) a

    INNER JOIN (SELECT * FROM sc WHERE sc.`Cid`=02) b

    ON a.Sid=b.Sid;

    -- 1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

    SELECT * FROM

    (SELECT * FROM sc WHERE sc.`Cid`=01) a

    LEFT JOIN (SELECT * FROM sc WHERE sc.`Cid`=02) b

    ON a.Sid=b.Sid;

    -- 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

    SELECT b.* FROM

    (SELECT * FROM sc WHERE sc.`Cid`=02) b

    LEFT JOIN

    (SELECT * FROM sc WHERE sc.`Cid`=01) a

    ON b.Sid=a.Sid

    WHERE a.Sid IS NULL;

    -- 2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

    SELECT * FROM student stu

    INNER JOIN

    (SELECT sc.Sid,AVG(score) AS avgscore FROM sc GROUP BY Sid) a

    ON stu.`Sid`=a.Sid

    WHERE a.avgscore > 60

    -- 3. 查询在 sc 表存在成绩的学生信息

    SELECT * FROM

    sc LEFT JOIN

    student stu

    ON sc.`Sid`=stu.`Sid`

    WHERE sc.`score` IS NOT NULL;

    -- 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

    SELECT * FROM student stu

    LEFT JOIN

    (SELECT Sid,COUNT(Cid),SUM(score) FROM sc GROUP BY Sid) b

    ON stu.`Sid`=b.Sid

    -- 4.1 查有成绩的学生信息

    SELECT DISTINCT stu.Sname,stu.`Sage`,stu.`Ssex`

    FROM sc

    LEFT JOIN student stu ON stu.`Sid`=sc.`Sid`

    WHERE sc.`score` IS NOT NULL;

    -- 5. 查询「李」姓老师的数量

    SELECT COUNT(*) '「李」姓老师的数量'

    FROM teacher

    WHERE Tname LIKE '李四%'

    -- 6. 查询学过「张三」老师授课的同学的信息

    SELECT * FROM student stu

    WHERE stu.Sid IN

    (SELECT Sid FROM course

    LEFT JOIN sc

    ON course.`Cid`=sc.`Cid`

    WHERE course.Tid

    IN(SELECT Tid FROM teacher

    WHERE teacher.`Tname`='张三'))

    -- 7. 查询没有学全所有课程的同学的信息

    SELECT * FROM student stu

            WHERE stu.Sid IN (SELECT stu.Sid  FROM student stu

    LEFT JOIN sc

    ON stu.Sid=sc.Sid

    WHERE sc.`Cid` IS NULL)

    -- 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

    #1

    SELECT * FROM student stu

    WHERE stu.Sid IN (SELECT DISTINCT sc.Sid FROM sc

    WHERE sc.`Cid` IN (SELECT course.Cid FROM course

    WHERE course.`Cid` IN (SELECT sc.`Cid` FROM sc

    WHERE sc.Sid=01)))

    #2

    SELECT * FROM student WHERE Sid IN (SELECT Sid FROM sc WHERE Cid IN (SELECT Cid FROM sc WHERE Sid = '01'));

    -- 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

    #01号同学的课程

    SELECT sc.`Cid` FROM sc

    WHERE sc.`Sid`=01

    #02不在以上课程中的同学

    SELECT sc.`Sid` FROM sc

    WHERE sc.`Cid` NOT IN (SELECT sc.`Cid` FROM sc

    WHERE sc.`Sid`=01)

    #数据02子集的同学

    SELECT * FROM sc

    WHERE sc.`Sid` NOT IN(

    SELECT sc.`Sid` FROM sc

    WHERE sc.`Cid` NOT IN (SELECT sc.`Cid` FROM sc

    WHERE sc.`Sid`=01)

    );

    #痛过grouby将所有数据按照sid进行分组,并计算出每组的sid数量

    SELECT DISTINCT sc.Sid FROM sc

    WHERE sc.Sid IN(

    SELECT sc.Sid FROM sc

    WHERE sc.`Sid` NOT IN(

    SELECT sc.`Sid` FROM sc

    WHERE sc.`Cid` NOT IN (SELECT sc.`Cid` FROM sc

    WHERE sc.`Sid`=01))

    GROUP BY Sid

    HAVING COUNT(*)=(SELECT COUNT(*) FROM sc WHERE sc.`Sid`=01) AND sc.Sid!=01);

    #查上面所查到的sid的信息

    SELECT * FROM student

    WHERE student.`Sid` IN (

    SELECT DISTINCT sc.Sid FROM sc

    WHERE sc.Sid IN(

    SELECT sc.Sid FROM sc

    WHERE sc.`Sid` NOT IN(

    SELECT sc.`Sid` FROM sc

    WHERE sc.`Cid` NOT IN (SELECT sc.`Cid` FROM sc

    WHERE sc.`Sid`=01))

    GROUP BY Sid

    HAVING COUNT(*)=(SELECT COUNT(*) FROM sc WHERE sc.`Sid`=01) AND sc.Sid!=01));

    -- 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名

    EXPLAIN SELECT * FROM (SELECT DISTINCT Sid FROM sc

    WHERE sc.`Sid` NOT IN (SELECT Cid FROM course c

    LEFT JOIN teacher t

    ON c.`Tid`=t.`Tid`

    WHERE t.`Tname`='张三')) mx LEFT JOIN student

    ON  mx.Sid=student.`Sid`

    EXPLAIN SELECT SQL_NO_CACHE * FROM student

    WHERE student.Sid IN (SELECT DISTINCT Sid FROM sc

    WHERE sc.`Sid` NOT IN (SELECT Cid FROM course c

    LEFT JOIN teacher t

    ON c.`Tid`=t.`Tid`

    WHERE t.`Tname`='张三'));

    CREATE INDEX sc_index IN sc

    -- 11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

    #1查询两门以上不及格的sid和平均值

    SELECT sc.`Sid`,AVG(score) FROM sc

    WHERE sc.`score` < 60

    GROUP BY sc.`Sid`

    HAVING COUNT(*) >=2

    #2联表student

    SELECT a.score,a.Sid,stu.Sname FROM (SELECT sc.`Sid`,AVG(score) score FROM sc

    WHERE sc.`score` < 60

    GROUP BY sc.`Sid`

    HAVING COUNT(*) >=2) a

    LEFT JOIN student stu

    ON a.Sid = stu.Sid

    -- 12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

    SELECT * FROM (SELECT * FROM sc

    WHERE sc.`Cid`=01 AND sc.`score` < 60) a

    LEFT JOIN student stu

    ON a.`Sid`=stu.Sid

    ORDER BY a.score DESC;

    -- 13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

    SELECT DISTINCT sc.`Sid`,a.av FROM sc

    LEFT JOIN (SELECT Sid,AVG(sc.`score`) av FROM sc

    GROUP BY sc.`Sid`) a

    ON sc.`Sid`=a.Sid

    ORDER BY a.av

    -- 14. 查询各科成绩最高分、最低分和平均分:

    SELECT * FROM course cou

    LEFT JOIN (SELECT sc.`Cid`,MAX(score),MIN(score),AVG(score) FROM sc

    GROUP BY sc.`Cid`) b

    ON cou.Cid=b.Cid

    -- 15. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

    -- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

    #01将各分数线分类

    SELECT

    CASE WHEN score >= 60 AND score < 70 THEN '及格'

    WHEN score >= 70 AND score < 80 THEN '中等'

    WHEN score >= 80 AND score < 90 THEN '优良'

    WHEN score >= 90 THEN '优秀'

    ELSE  '不及格'

    END grade

    FROM sc

    #02进入主题

    SELECT * FROM course a

    LEFT JOIN(SELECT Cid,MAX(score) '最高分',MIN(score) '最低分',AVG(score) '平均分',

    SUM(CASE WHEN score >= 60 THEN 1 ELSE 0 END ) / COUNT(DISTINCT Sid) AS '及格率',

    SUM(CASE WHEN score >= 70 AND score < 80 THEN 1 ELSE 0 END ) / COUNT(DISTINCT Sid) '中等率',

    SUM(CASE WHEN score >= 80 AND score < 90 THEN 1 ELSE 0 END ) / COUNT(DISTINCT Sid) '优良率',

    SUM(CASE WHEN score >= 90 THEN 1 ELSE 0 END ) / COUNT(DISTINCT Sid) '优秀率'

    FROM sc

    GROUP BY Cid) b

    ON a.Cid=b.Cid

    -- 16. 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

    SELECT sc.`Cid`,COUNT(*) FROM sc

    GROUP BY sc.`Cid`

    ORDER BY COUNT(*) DESC,sc.`Cid` ASC;

    -- 15. 按各科成绩进行排序,并显示排名, score 重复时保留名次空缺

    #同score为null

    SET @rk=0;

    SET @last_Cid=0;

    SET @score=0;

    SELECT sc.`Cid`,sc.`score`,sc.`Sid`,IF(@last_Cid=sc.`Cid`,

    IF(@score=sc.`score`,NULL,@rk:=@rk+1),

    @rk:=1) AS rk,

    @last_Cid:=sc.`Cid` AS last_Cid,

    @score:=sc.`score` AS last_score

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC

    -- 15.1 按各科成绩进行排序,并显示排名, score 重复时合并名次(1,1,2,3,4...)

    #同分数并列排名,所有自定义变量都要打印,否则结果会是错的

    SET @rk=0;

    SET @last_Cid=0;

    SET @score=0;

    SELECT sc.`Cid`,sc.`score`,sc.`Sid`,IF(@id=sc.`Cid`,

    IF(@score=sc.`score`,@rk:=@rk,@rk:=@rk+1),

    @rk:=1) AS rk,

    @id:=sc.`Cid` AS id,

    @score:=sc.`score` AS last_score

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC

    -- 16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺

    SELECT sc.`Sid`,SUM(sc.`score`) sumscore FROM sc

    GROUP BY sc.`Sid`

    ORDER BY sumscore

    -- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

    -- 17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

    #01

    SELECT sc.`Cid`,

    SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[60-0]',

    SUM(CASE WHEN score >= 60 AND score < 70 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[70-60]',

    SUM(CASE WHEN score >= 70 AND score < 85 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[85-70]',

    SUM(CASE WHEN score >= 85 AND score < 100 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[100-85]'

    FROM sc

    GROUP BY sc.`Cid`;

    #02联表查询

    SELECT * FROM course a

    LEFT JOIN (SELECT sc.`Cid`,

    SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[60-0]',

    SUM(CASE WHEN score >= 60 AND score < 70 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[70-60]',

    SUM(CASE WHEN score >= 70 AND score < 85 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[85-70]',

    SUM(CASE WHEN score >= 85 AND score < 100 THEN 1 ELSE 0 END) / COUNT(DISTINCT Sid) '[100-85]'

    FROM sc

    GROUP BY sc.`Cid`) b

    ON a.Cid=b.Cid;

    -- 18. 查询各科成绩前三名的记录 使用自定义函数

    SET @rank=0;

    SET @last_Cid=0;

    SELECT a.Cid,a.Sid,a.score,a.rk FROM (SELECT Cid,Sid,score,

    IF(@last_Cid=Cid,@rank:=@rank+1,@rank:=1) AS rk,

    @last_Cid:=Cid

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC) a

    WHERE a.rk <=3

    -- 19. 查询每门课程被选修的学生数

    SELECT sc.`Cid`,COUNT(sc.`Sid`) FROM sc

    GROUP BY sc.`Cid`

    -- 20. 查询出只选修两门课程的学生学号和姓名

    SELECT * FROM student a

    WHERE a.Sid IN (SELECT sc.`Sid` FROM sc

    GROUP BY sc.`Sid`

    HAVING COUNT(DISTINCT sc.`Cid`)=2)

    -- 21. 查询男生、女生人数

    #1

    SELECT

    SUM(CASE WHEN Ssex='男' THEN 1 ELSE 0 END) '男',

    SUM(CASE WHEN Ssex='女' THEN 1 ELSE 0 END) '女'

    FROM student

    #2

    SELECT st.Ssex,COUNT(*) FROM student st

    GROUP BY st.Ssex

    -- 22. 查询名字中含有「风」字的学生信息

    SELECT * FROM student stu

    WHERE stu.Sname LIKE '%风%'

    -- 23. 查询同名同性学生名单,并统计同名人数

    #1

    SELECT a.Sname, a.Ssex,COUNT(*) FROM student a

    GROUP BY a.Sname, a.Ssex

    HAVING COUNT(*)>= 2

    #2

    SELECT c.Sname,c.Ssex, COUNT(DISTINCT c.Sid) AS counts FROM (

    SELECT a.Sid, a.Sname, a.Ssex FROM student a

    INNER JOIN student b

    ON a.Sname = b.Sname

    AND a.Ssex = b.Ssex

    ) c

    GROUP BY c.Sname, c.Ssex

    HAVING COUNT(c.Sid) >= 2;

    -- 24. 查询 1990 年出生的学生名单

    SELECT * FROM student stu

    WHERE YEAR(stu.`Sage`)='1990' AND MONTH(stu.Sage)='05'

    -- 25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

    SELECT sc.`Cid`,AVG(score) FROM sc

    GROUP BY sc.`Cid`

    ORDER BY AVG(score) DESC,sc.`Cid` ASC;

    -- 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

    #01查询每名同学的平均成绩

    SELECT Sid,AVG(sc.`score`) FROM sc

    GROUP BY sc.`Sid`

    #02进入正题

    SELECT * FROM student a

    INNER JOIN(SELECT Sid,AVG(sc.`score`) FROM sc

    GROUP BY sc.`Sid`

    HAVING AVG(sc.`score`) > 85 ) b

    ON a.`Sid`=b.Sid

    -- 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

    SELECT b.Sname,sc.`score` FROM course a

    LEFT JOIN sc

    ON a.Cid=sc.`Cid`

    LEFT JOIN student b

    ON SC.`Sid`=b.Sid

    WHERE a.Cname='数学' AND sc.`score`<60

    -- 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

    SELECT a.Sname,COUNT(*) '课程数量',IF(AVG(b.`score`),AVG(b.`score`),0) FROM student a

    LEFT JOIN sc b

    ON a.Sid=b.Sid

    GROUP BY a.Sname

    SELECT a.*, b.* FROM student a

    LEFT JOIN sc b

    ON a.Sid = b.Sid;

    -- 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

    SELECT b.`Sname`,c.`Cname`,a.score FROM

    (SELECT * FROM sc

    WHERE sc.`score` > 70) a

    LEFT JOIN student b

    ON a.`Sid`=b.Sid

    LEFT JOIN course c

    ON a.Cid=c.`Cid`

    -- 30. 查询不及格的课程

    SELECT a.Sid,c.Sname,b.Cname,a.Score FROM sc a

    LEFT JOIN course b

    ON a.Cid=b.Cid

    LEFT JOIN student c

    ON a.Sid=c.Sid

    WHERE a.score < 60

    -- 31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

    SELECT a.Sid,b.Sname FROM student a

    LEFT JOIN sc b

    ON a.Sid=b.`Sid`

    WHERE b.`Cid`=01 AND b.`score`>80

    -- 32. 求每门课程的学生人数

    SELECT Cid,COUNT(DISTINCT Sid) FROM sc a

    GROUP BY a.`Cid`;

    -- 33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

    #1

    SELECT d.*,e.score FROM student d

    LEFT JOIN(SELECT DISTINCT c.Sid,c.`score` FROM course a

    LEFT JOIN teacher b

    ON a.`Tid`=b.`Tid`

    LEFT JOIN sc c

    ON a.Cid=c.Cid

    WHERE b.`Tname`='张三') e

    ON d.Sid=e.Sid

    ORDER BY e.score DESC

    LIMIT 1

    #2

    SELECT * FROM (

    SELECT a.Sid, MAX(a.score) AS maxscore, b.Sname, rank() over(ORDER BY a.score DESC) AS rk FROM sc a

    LEFT JOIN student b

    ON a.Sid = b.Sid

    WHERE Cid = (

    SELECT Cid FROM course WHERE Tid = (SELECT Tid FROM teacher WHERE Tname = '张三'))

    GROUP BY Sid)c

    WHERE c.rk = 1;

    -- 34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

    SELECT * FROM (

    SELECT a.Sid, MAX(a.score) AS maxscore, b.Sname,dense_rank() over(ORDER BY a.score DESC) AS rk FROM sc a

    LEFT JOIN student b

    ON a.Sid = b.Sid

    WHERE Cid = (

    SELECT Cid FROM course WHERE Tid = (SELECT Tid FROM teacher WHERE Tname = '张三'))

    GROUP BY Sid)c

    WHERE c.rk = 1;

    -- 35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

    SELECT DISTINCT d.`Sname`,c.`Cname`,a.`score` FROM sc a

    INNER JOIN sc b

    ON a.`score`=b.`score` AND a.`Cid`!=b.`Cid`

    LEFT JOIN course c

    ON b.`Cid`=c.`Cid`

    INNER JOIN student d

    ON b.Sid=d.Sid

    -- 36. 查询每门功课成绩最好的前两名

    #1排序错的,未查找到原因

    SET @rank=0;

    SET @last_Cid=0;

    SELECT *,

    IF (@last_Cid=b.`Cid`,@rank:=@rank+1,@rank:=1) AS rk,

    @last_Cid:=b.`Cid` AS last_Cid

    FROM course2 a

    LEFT JOIN sc2 b

    ON a.`Cid`=b.`Cid`

    ORDER BY b.Cid,b.`score` DESC

    #正确的方式

    SELECT a.Sid,b.Sname,a.`Cid` FROM(SELECT sc.`Cid`,sc.`score`,sc.`Sid`,

    IF(@id=sc.`Cid`,@rk:=@rk+1,@rk:=1) AS rk,

    @id:=sc.`Cid` AS id

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC) a

    LEFT JOIN student b

    ON a.Sid=b.Sid

    WHERE a.rk <=2

    #下面同score为null

    SET @rk=0;

    SET @last_Cid=0;

    SET @score=0;

    SELECT sc.`Cid`,sc.`score`,sc.`Sid`,IF(@last_Cid=sc.`Cid`,

    IF(@score=sc.`score`,null,@rk:=@rk+1),

    @rk:=1) AS rk,

    @last_Cid:=sc.`Cid` AS last_Cid,

    @score:=sc.`score` AS last_score

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC

    #同分数并列排名,所有自定义变量都要打印,否则结果会是错的

    SET @rk=0;

    SET @last_Cid=0;

    SET @score=0;

    SELECT sc.`Cid`,sc.`score`,sc.`Sid`,IF(@id=sc.`Cid`,

    IF(@score=sc.`score`,@rk:=@rk,@rk:=@rk+1),

    @rk:=1) AS rk,

    @id:=sc.`Cid` AS id,

    @score:=sc.`score` AS last_score

    FROM sc

    ORDER BY sc.`Cid`,sc.`score` DESC

    -- 37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。

    SELECT sc.`Cid`,COUNT(*) FROM sc

    GROUP BY sc.`Cid`

    HAVING COUNT(sc.`Sid`)>5

    -- 38. 检索至少选修两门课程的学生学号

    SELECT  sc.`Sid`,COUNT(DISTINCT sc.`Cid`) FROM sc

    GROUP BY sc.`Sid`

    HAVING COUNT(DISTINCT sc.`Cid`) >= 2

    -- 39. 查询选修了全部课程的学生信息

    SELECT * FROM sc a

    LEFT JOIN student b

    ON a.Sid=b.`Sid`

    GROUP BY a.`Sid`

    HAVING COUNT(DISTINCT a.`Cid`)=(SELECT COUNT(DISTINCT Cid) FROM sc )

    -- 40. 查询各学生的年龄,只按年份来算

    SELECT (YEAR(CURDATE()) - YEAR(student.Sage) + 1) FROM student;

    -- 41. 按照出生日期来算,当前月日 < 出生年月的月日则年龄减一

    SELECT student.`Sid`,IF(YEAR(CURDATE()) < YEAR(student.Sage),(YEAR(CURDATE()) - YEAR(student.Sage) - 1),(YEAR(CURDATE()) - YEAR(student.Sage) + 1)) AS '年龄' FROM student;

    -- 42. 查询本周过生日的学生

    SELECT stu.`Sid`,stu.`Sname` FROM student stu

    WHERE WEEK(stu.`Sage`)=WEEK(NOW());

    -- 43. 查询下周过生日的学生

    SELECT stu.`Sid`,stu.`Sname` FROM student stu

    WHERE WEEK(stu.`Sage`)=WEEK(NOW())+1;

    -- 44. 查询本月过生日的学生

    SELECT stu.`Sid`,stu.`Sname` FROM student stu

    WHERE MONTH(stu.`Sage`)=MONTH(NOW());

    -- 45. 查询下月过生日的学生

    SELECT stu.`Sid`,stu.`Sname` FROM student stu

    WHERE MONTH(stu.`Sage`)=MONTH(NOW())+1;

    相关文章

      网友评论

        本文标题:Mysql Sql 语句练习题mysql 数据库练习题

        本文链接:https://www.haomeiwen.com/subject/jpllhhtx.html