美文网首页数据蛙数据分析每周作业
50道SQL练习题(未完待续)

50道SQL练习题(未完待续)

作者: Hill_GM | 来源:发表于2019-01-13 19:38 被阅读10次

    测试表格

    --1.学生表

    Student(S,Sname,Sage,Ssex) 

    --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

    --2.课程表 

    Course(C,Cname,T) 

    --C --课程编号,Cname 课程名称,T 教师编号

    --3.教师表 

    Teacher(T,Tname)

     --T 教师编号,Tname 教师姓名

    --4.成绩表 

    SC(S,C,score)

     --S 学生编号,C 课程编号,score 分数

    数据库信息

    学生表 Student

    create table Student(S varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));

    insert into Student values('01' , '赵雷' , '1990-01-01' , '男');

    insert into Student values('02' , '钱电' , '1990-12-21' , '男');

    insert into Student values('03' , '孙风' , '1990-05-20' , '男');

    insert into Student values('04' , '李云' , '1990-08-06' , '男');

    insert into Student values('05' , '周梅' , '1991-12-01' , '女');

    insert into Student values('06' , '吴兰' , '1992-03-01' , '女');

    insert into Student values('07' , '郑竹' , '1989-07-01' , '女');

    insert into Student values('08' , '王菊' , '1990-01-20' , '女');

    科目表 Course

    create table Course(C varchar(10),Cname nvarchar(10),T varchar(10));

    insert into Course values('01' , '语文' , '02');

    insert into Course values('02' , '数学' , '01');

    insert into Course values('03' , '英语' , '03');

    教师表 Teacher

    create table Teacher(T varchar(10),Tname nvarchar(10));

    insert into Teacher values('01' , '张三');

    insert into Teacher values('02' , '李四');

    insert into Teacher values('03' , '王五');

    成绩表 SC

    create table SC(S varchar(10),C varchar(10),score decimal(18,1));

    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);

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

    select * from (selectstudent.*,c,score from student,sc where student.s=sc.s and c='01')c,(selectstudent.*,c,score from student,sc where student.s=sc.s and c='02')d wherec.s=d.s and c.score >d.score;

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

    Select *from (select * from sc where sc.c='01')A inner join (select * from sc wheresc.c='02')B on A.s=B.s;

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

    Select * from (select * fromsc where sc.c='01')A left join (select * from sc where sc.c='02')B on A.s=B.swhere B.c is null;

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

    Select * from (select * fromsc where sc.c='01')A right join (select * from sc where sc.c='02')B on A.s=B.swhere A.c is null;

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

    selectst.s,st.sname,avg(score) from student as st,sc where st.s=sc.s group by st.shaving avg(score)>=60;

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

    Select * from student asst,sc where st.s=sc.s;

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

    Select A.s,sname,count(c)选课总数,score  总成绩 from student A left join sc B on A.s=B.s group by A.s;

    4.1查有成绩的学生信息

    Select A.s,sname,count(c)选课总数,score  总成绩 from student A inner join sc B on A.s=B.s group by A.s ;

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

    Select count(Tname) fromTeacher  where Tname like '李%' group by Tname;

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

    Select student.* from studentwhere s in (select s from sc where c in (select c from course where t in(select t from teacher where tname ='张三')));

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

    Select * from student asst,sc where st.s=sc.s group by st.s having count(st.sname)< 3;

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

    Select * from (Select st.*from student as st,sc where st.s=sc.s and sc.c= any (select c from sc where s='01'))A group by s;

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

    Select st.* from student as

    st,sc where st.s=sc.s group by st.s having st.s<>'01' and sum(c)=(select sum(c) from sc where s='01' group by s);

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

    Select st.* from student asst,sc where st.s=sc.s and sc.c in (select c from course where t = any (select tfrom teacher where tname='张三'));

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

    select A.* from student A ,(select * from sc where score <60 group by s having count(s)>=2)B whereA.s = B.s;

    or

    select A.* from student Ainner join (select * from sc where score <60 group by s havingcount(s)>=2)B on A.s = B.s;

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

    Select st.* from student asst,(select * from sc where c='01' and score<60)A where st.s=A.s order byscore desc;

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

    Select sc.*,平均值from sc left join (select *,avg(score) as平均值from scgroup by s)A on sc.s=A.s order by平均值 desc;

    相关文章

      网友评论

        本文标题:50道SQL练习题(未完待续)

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